Re:The Curious Case of JavaScript’s `sort`
by xlm1717 on Wednesday April 13, 2016 @11:20AM
(#11488915)
It might have made more sense to not have default behavior and force the user to specify how they want to sort. They could also have done what PHP did and provided several different .sort functions depending on how you want to sort.
Re:The Curious Case of JavaScript’s `sort`
by fenomas on Wednesday April 13, 2016 @11:33AM
(#11489045)
I think ECMA4 proposed behavior flags, so you could do:
arr1.sort( Array.NUMERICAL | Array.DESCENDING )
arr2.sort( Array.CASE_INSENSITIVE )
or similar.
Re:The Curious Case of JavaScript’s `sort`
by CJefferson on Wednesday April 13, 2016 @11:45AM
(#11489173)
What would make sense (and what most other languages seem to do) is compare elementwise, using whatever ordering javascript would usually use. Therefore [X] < [Y] would be true whenever X<Y is true (for example)
Re:The Curious Case of JavaScript’s `sort`
by fenomas on Wednesday April 13, 2016 @12:17PM
(#11489532)
That would have undesirable side effects - e.g. it would mean the results of the sort are not necessarily specified. If the array contains NaN, for example, calling a sort like that would have different results depending on the order in which the elements are compared, which would likely vary by JS engine.
Re:The Curious Case of JavaScript’s `sort`
by CJefferson on Wednesday April 13, 2016 @12:20PM
(#11489559)
I don't really understand your problem -- this is how array comparison is implemented in C++, Python, Ruby, Java (when using the default isComparable on most array-list types) and Haskell (from the languages I use frequently). Javascript is the only weird outlier.
Re:The Curious Case of JavaScript’s `sort`
by fenomas on Wednesday April 13, 2016 @12:45PM
(#11489846)
I don't know what you're taking issue with. The results of sorting in JS with a simple (a<b) comparison will necessarily vary by JS implementation, and I'm suggesting that would be a drawback for a default specified behavior.
E.g.:
[1,3,NaN,2,5].sort( (a,b) => a<b ? -1 : 1 )
// Chrome: [ NaN, 1, 2, 3, 5 ]
// Firefox: [ 2, 5, NaN, 1, 3 ]
Re:The Curious Case of JavaScript’s `sort`
by CJefferson on Wednesday April 13, 2016 @01:16PM
(#11490145)
I am refering to this:
In (almost) every other language ever the following is true. In Javascript it is false.
[2] < [11]
This means:[[2], [11]].sort() // Javascript: [ [11], [2] ]
Re:The Curious Case of JavaScript’s `sort`
by fenomas on Wednesday April 13, 2016 @02:02PM
(#11490615)
You're overlapping a couple of separate things. The article and my initial comment were about sorting arrays of numbers. If you're sorting arrays of arrays, the behavior of ([2] < [11]) is slightly beside the point, since Array.sort() doesn't compare the arrays, it converts all elements to strings first.
As for the actual behavior of ([2] < [11]), the issue there is that "Array" is not a type in JS, arrays are just objects that happen to have properties called "0" and "1" and so on. Hence operators don't have any special cases for arrays, so getting comparisons to work like you had hoped would mean the comparison operator would need weird special rules where, whenever it's passed two objects it checks if they both have properties called "0", and if so do one thing, if not do something else, etc.
Re:The Curious Case of JavaScript’s `sort`
by gpvos on Wednesday April 13, 2016 @03:41PM
(#11491515)
That still wouldn't specify how to sort [ 1, [ 5 ], "hi", { a: 5 }, new Date(), /test/ ] , and which way to sort that would "make sense" anyway? Better to specify something simple but incomplete than something complicated that will still necessarily be either incomplete or useless or both.
It may seem like a strange way to spec the language, but I'm not sure what would have been better. It wouldn't make sense for sort to have to run through the array checking types before it starts, or for it to dynamically change what kind of comparison it does depending on the types of each pair of elements being compared.