Sort arrays in ascending and descending order
JavaScript code snippet on how to use the Array sort() method to sort arrays of numbers, strings, and objects.
// Ascendingconst sortAscending = (array) => array.sort((a, b) => a - b);// Descendingconst sortDescending = (array) => array.sort((a, b) => b - a);
How it works
The JavaScript Array sort method has a default process of sorting items in ascending order, which is accomplished by converting the elements into strings and comparing their sequences of UTF-16 code unit values.