Re:The Curious Case of JavaScript’s `sort`
by CJefferson on Wednesday April 13, 2016 @11:38AM
(#11489095)
Lexicographic ordering of two arrays X and Y is (I think) the standard ordering of arrays. Algorithm something like (pseudocode, ignoring different length arrays):
for(i = 0; i < length(X); ++i) {
if(X[i] < Y[i]) X is smaller
if(X[i] > Y[i]) Y is smaller
}
arrays are equal!
In every programming language I've ever used (other than Javascript), the following is how arrays are ordered (well, except for languages like C, where they are compared by memory location by default)
[1,1] < [2,2] < [11,11] < [22,22]
Instead Java says [1,1] < [11,11] < [2,2] < [22,22], because it compares the arrays as strings.
[1,1] < [2,2] < [11,11] < [22,22]
Instead Java says [1,1] < [11,11] < [2,2] < [22,22], because it compares the arrays as strings.