3 ms·
I was looking recently and found there doesn't seem to be any way to do lexicographic ordering of lists in JavaScript, unlike (almost) every other language I've
by CJefferson 3y ago
I was looking recently and found there doesn't seem to be any way to do lexicographic ordering of lists in JavaScript, unlike (almost) every other language I've ever used, where lists are ordered lexicographically by default.
Is there any magic quick way of doing this in modern JavaScript, or do I have to keep cutting+pasting a comparator I wrote everywhere?
- johnfn 3y agoHm I don't understand what you mean. What's wrong with `myList.sort()`?
- CJefferson 3y agoSorry, I mean the things I'm sorting are themselves lists. Imagine for example: [ [1,23], [11,1], [11,44], [22,4] ] I'd consider this list "sorted", as the inner lists are sorted lexicographically. Except, if I gave this list to Javascript and asked it to sort it, it would cast each of the inner lists to strings, which (for example) leads to [11,11] < [1,1], as "[11,11]" < "[1,1]"
- TurningCanadian 3y agoString's localeCompare: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...