About array sorting and duplicates to deal with

sort the array by sort. No matter what the sort is, it needs to be sorted from 1 according to the previous size order. Sort repeatedly forgets the back row according to the previous order, asking for great advice, and there are problems with several methods.

let arr = [{sort:-2, id: 9},{sort:0, id: 10},{sort:1, id:8},{sort:2, id:1},{sort:4, id: 5},{sort:4, id: 7},{sort:6, id: 6},];
let arrNew = [{sort:1, id: 9},{sort:2, id: 10},{sort:3, id:8},{sort:4, id:1},{sort:5, id: 5},{sort:6, id: 7},{sort:7, id: 6},]
Mar.04,2021

is your arr already sorted?

let arrNew = arr.map((v, i) => (v.sort = i + 1, v))

stable sort, first and then use @ Li Shisan's code

// import Lodash...
_.sortBy(arr, [o => o.sort]);
let arrNew = arr.map((v, i) => (v.sort = i + 1, v));
Menu