Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort JavaScript strings by codePoint?

I am looking to sort objects by a string field that contains unicode characters. However, I want to sort the strings by code point, not by locale. So, here is an example, where JavaScript sorts objects so that \u24B7 and b are both considered the same character.

Incorrect sort order:

> [{name: 'a'}, {name: 'b'}, {name: 'd'}, {name: '\u24B7'}].sort((a,b)=> a.name.localeCompare(b.name))
[ { name: 'a' }, { name: 'b' }, { name: 'Ⓑ' }, { name: 'd' } ]

However, this is not what I want. I want the following sort order, where they are considered different characters. This is the default behavior when comparing strings and not including a comparator function.

Correct sorting order (notice that b and \u24B7 are no longer considered the same sort character):

> ['a','b','\u24B7','d'].sort()
[ 'a', 'b', 'd', 'Ⓑ' ]

In the real application, the strings will be more than one character and may contain multiple unicode chars and we want them sorted according to unicode number (ie- code point).

My question: is there a simple way to sort by code point for strings? I'd rather not re-implement a custom comparator for this.

like image 526
Andrew Eisenberg Avatar asked Oct 29 '25 06:10

Andrew Eisenberg


1 Answers

I usually do it like this:

let cmp = (a, b) => a > b ? 1 : a < b ? -1 : 0;

objects.sort((a, b) => cmp(a.name, b.name));

or rather

let sortBy = (a, f) => a.sort((x, y) => cmp(f(x), f(y)));

sortBy(objects, x => x.name);
like image 136
georg Avatar answered Oct 31 '25 21:10

georg



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!