EN
JavaScript - sort array of strings
3
points
In this article, we would like to show you how to sort array of strings using JavaScript.
Quick solution:
// ONLINE-RUNNER:browser;
const array = ['C', 'A', 'B'];
array.sort();
console.log(array); // ['A', 'B', 'C']
Sort in reverse order
To sort the array in reverse order, we need to change comparer.
// ONLINE-RUNNER:browser;
const array = ['C', 'A', 'B'];
array.sort((a, b) => -a.localeCompare(b));
console.log(array); // ['C', 'B', 'A']