Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localeCompare options in nodejs unexpected result

This is from a node cli session:

> var a = 'foo12';
undefined
> var b = 'foo3';
undefined
> var s = [a , b];
undefined
> s.sort(function(a, b) {
... return a.localeCompare(b, 'en', { numeric: true });
... });
[ 'foo12', 'foo3' ]

This is from the Chrome console:

var a = 'foo12';
undefined
var b = 'foo3';
undefined
var s = [a,b]
undefined
s.sort(function(a,b) {return a.localeCompare(b, 'en', {numeric:true});})
["foo3", "foo12"]

I.e., the numeric: true option for natural sorting seems to be a no-op in my setup for node.

Is there a way to get node to behave better in this situation, or an explanation as to why it does not?

like image 235
bcr Avatar asked Mar 18 '26 20:03

bcr


1 Answers

This is the best sort-of-answer I've found so far: https://github.com/joyent/node/issues/7676

TL;DR v8 doesn't concern itself with supporting localeCompare fully, but chrome uses v8-i18n to support this. Node is in the process of figuring out how to incorporate full support into Node.

like image 130
bcr Avatar answered Mar 20 '26 10:03

bcr



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!