Typescript - Sort strings descending
A more current answer is that you can utilize String.prototype.localCompare() to get a numeric comparison value
Simple example:
let values = ["Saab", "Volvo", "BMW"];
values.sort((a, b) => b.localeCompare(a))
This also wont causes TypeScript warnings as the output of localCompare
is a number
More info and additional function parameters can be seen here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
You need to create a comparison function and pass it as a parameter of sort function:
values.sort((one, two) => (one > two ? -1 : 1));
Use the following code to sorting the Array in ascending and descending order.
const ascending: any= values.sort((a,b) => (a > b ? 1 : -1));
const descending: any= values.sort((a,b) => (a > b ? -1 : 1))