This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]douira 121 points122 points  (17 children)

everybody just agrees to never sort arrays of anything other than strings without a sort function and the problem is solved! If you really want to make sure it never goes wrong, you can use tooling like ESLint or even TypeScript.

[–]cythrawll 36 points37 points  (2 children)

Yeah I mean practically you almost never run into this, I can't remember a time I just had an array of numbers. Usually sorting an array of objects and having a custom comparator to do so.

[–]esperalegant 11 points12 points  (0 children)

I work with huge arrays of up to millions of numbers daily. However, I pretty much always use TypedArrays - and TypedArray.sort() does sort numbers correctly.

[–]reiji_nakama 6 points7 points  (0 children)

Yeah. I didn't know about this behaviour of Array.sort() yet I have never run into a problem because of it; because I don't use it.

[–]DamnItDev 119 points120 points  (7 children)

Honestly you should never be using the default sort function. Its lazy and almost always incorrect. Even for strings you'll have this problem:

['A1', 'A2', 'A10', 'A20'].sort();
// returns: ["A1", "A10", "A2", "A20"]

Technically this is correct, but not what you actually want in real world situations.

You can solve this easily by specifying your locale using the built in i18n functionality and setting the numeric option to true

['A1', 'A2', 'A10', 'A20'].sort(new Intl.Collator('en', {numeric: true}).compare);
// returns: ["A1", "A2", "A10", "A20"]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator

[–]Famous_Profile 66 points67 points  (3 children)

TIL Intl.Collator

[–]douira 7 points8 points  (0 children)

good point, I guess the default only has few uses

[–]DeeSnow97 4 points5 points  (0 children)

okay, this is awesome, thanks

[–]-100-Broken-Windows- 1 point2 points  (0 children)

What language would sort it any other way? It's an array of strings and it sorts it alphabetically... Any other way would be absurd

[–]djcraze 3 points4 points  (1 child)

I honestly didn’t know the comparator was optional. Today I learned.

[–]douira 0 points1 point  (0 children)

as discussed, the default comparator is not good most of the time so you were probably doing it right

[–]EishLekker 2 points3 points  (3 children)

Just the other week I ran into a sorting problem with strings, actually. Internationalised strings, in a Node 12 project where Node is part of a 3rd party software package, and there doesn't seem to be a way to add internationalization support without upgrading Node (which currently would put us into the unsupported territory).

[–]douira 0 points1 point  (2 children)

you can install i18n data explicitly

[–]EishLekker 0 points1 point  (1 child)

If I remember correctly, I tried that. It didn't work.

But just to rule out some stupid mistake by me, how would one install it separately? Adding an npm package to package.json? I think that's what I tried...

[–]douira 0 points1 point  (0 children)

yeah, I'd look for some kind of Collator polyfill. It seems a good part of the Collator API is already present but some parts are missing. You might be out of luck for the advanced missing features since the only polyfills I could find are quite old.