Sort arrays in ascending and descending order

By Hunter Becton on January 21, 2023

// Ascending
const sortAscending = (array) => array.sort((a, b) => a - b);

// Descending
const 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.