I\'m trying to sort an array [3,3,2,1,3,2,2,2,1] to [1,1,3,3,3,2,2,2,2].
[3,3,2,1,3,2,2,2,1]
[1,1,3,3,3,2,2,2,2]
I\'m trying to handle it using object, using the number as key, and th
You can do this
const sortNums = (arr) => { const result = {} for (let i = 0; i < arr.length; i++) { const num = result[arr[i]] || 0; result[arr[i]] = num + 1; } const a = []; for(let i = 0; i <= 9; i++) { if(result[i]) { a.push(...Array.from({length: result[i]}, x => i)); } } return a; }