How to sort strings descending with TypeScript?

Sometimes, we want to sort strings descending with TypeScript

In this article, we’ll look at how to sort strings descending with TypeScript.

How to sort strings descending with TypeScript?

To sort strings descending with TypeScript, we can compare strings with comparison operators.

For instance, we write

const sorted: string[] = values.sort((one, two) => (one > two ? -1 : 1));

to call sort with a callback to compare strings one and two.

We return -l to reverse their order if one is bigger than two.

Otherwise, we return 1 to sort them in ascending order.

one and two are both entries in values being iterated through.

Conclusion

To sort strings descending with TypeScript, we can compare strings with comparison operators.