Sort arrays the right way
sort() compares as text by default, so 10 comes before 9. Give it a compare function.
JavaScript · Sort arrays the right wayBeginner
const nums = [10, 9, 1, 100];
nums.slice().sort(); // [1, 10, 100, 9] (text order!)
const asc = nums.slice().sort((a, b) => a - b); // [1, 9, 10, 100]
const desc = nums.slice().sort((a, b) => b - a);
const people = [{ n: 'Sara', age: 25 }, { n: 'Ali', age: 30 }];
people.sort((a, b) => a.n.localeCompare(b.n)); // by name, language awaresortcomparearray