Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intl.Collator descending order sort

Tags:

javascript

Given this example:

let a = ['New York', 'New Hampshire', 'Maryland'];
let collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
a.sort(collator.compare);

How might this array be sorted in descending order?

like image 996
user1608142 Avatar asked Nov 19 '25 07:11

user1608142


1 Answers

You can switch the parameters:

a.sort( (x, y) => collator.compare(y, x) )

or sort and reverse:

a.sort(collator.compare).reverse()
like image 200
Slai Avatar answered Nov 20 '25 20:11

Slai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!