简体   繁体   中英

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 \Ⓑ 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 \Ⓑ 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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM