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));
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c075-4d97c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c075-4d97c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?