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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With