1

I have got a very strange result trying to sort an array:

[0,1,2,3,4,5,6,7,8,9,10].sort(function(a,b) {return a > b;})

Result:

[5, 0, 1, 2, 3, 4, 6, 7, 8, 9, 10]

I would like to understand, why is this result return? I know, that sort function should be written like this:

[0,1,2,3,4,5,6,7,8,9,10].sort(function(a,b) {return a - b;})
Evgeny Kuznetsov
  • 2,132
  • 2
  • 17
  • 27
  • 1
    works fine for me – Lelio Faieta Mar 20 '17 at 12:55
  • 1
    The result is probably implementation dependant. – Fabian Klötzl Mar 20 '17 at 12:56
  • @LelioFaieta It's not that it doesn't work, it's that the OP wants to know why `a > b` results in `5` being at the start. – James Thorpe Mar 20 '17 at 12:56
  • Read the documentation for `Array#sort`. You might as well ask what the result of `.sort(() => Math.random - 0.5)` was. –  Mar 20 '17 at 12:56
  • 4
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort Does not expect a Boolean – epascarello Mar 20 '17 at 12:57
  • @JamesThorpe my comment was to say that I cannot reproduce the issue. The sort works fine with both codes on my machine – Lelio Faieta Mar 20 '17 at 12:57
  • 2
    I'm not sure _why_ it messes up, but you can expand the `a>b` to return only 0 or 1, no -1, and get the same results. `[0,1,2,3,4,5,6,7,8,9,10].sort(function(a,b) {return (a-b) > 0 ? 1 : 0;})` – rlemon Mar 20 '17 at 12:57
  • 2
    your first sort function is not kind of symetrical in the way as it should be: `sortFn(a, b) === -sortFn(b, a)`, because you get only values of `0` and `1` (the numerical value). – Nina Scholz Mar 20 '17 at 13:03
  • Why is the first result strange? Your sort comparator doesn't return what it is supposed to, and so the fact that the result isn't properly sorted is not a surprise. `.sort(function() { return "hello" })` doesn't sort properly either, but so what? – nnnnnn Mar 20 '17 at 13:14
  • I suppose, that deep understanding of how javascript sort method works isn't a bad thing. I just want to understand, which sorting algorithms is applied and how they work. For arrays with up to 10 numbers, bubble sort is applied, if I understood correctly. If we use `.sort(function() { return "hello" })` we will get `[5, 10, 0, 9, 8, 7, 6, 1, 4, 3, 2]`, because `return "hello"` == `return true` == `return 1`. May be I am wrong. – Evgeny Kuznetsov Mar 20 '17 at 13:46
  • 1
    useful: http://stackoverflow.com/questions/7559608/median-of-three-values-strategy – georg Mar 20 '17 at 14:04

1 Answers1

1

Yes you could get strange sorting results, for example in Chrome, where the sorting starts with the first and last element and goes on with the middle element.

Basicall you get the following protocoll

                  return  needed
  a    b   a > b   value   value  comment
---  ---  ------  ------  ------  -----------
  0   10   false       0      -1  wrong value
  0    5   false       0      -1  wrong value
  2    0    true       1       1
  9    0    true       1       1
  8    0    true       1       1
  7    0    true       1       1
  6    0    true       1       1
  1    0    true       1       1
  4    0    true       1       1
  3    0    true       1       1
  2    3   false       0      -1  wrong value
  3    4   false       0      -1  wrong value
  4    1    true       1       1
  3    1    true       1       1
  2    1    true       1       1
  4    6   false       0      -1  wrong value
  6    7   false       0      -1  wrong value
  7    8   false       0      -1  wrong value
  8    9   false       0      -1  wrong value
  9   10   false       0      -1  wrong value

Basically you get a value for equal items, bu you have unequal items to compare.

You need a stable sort function which is kind of symetrical in the way as it yields the nevgative value with switched parameters.

sortFn(a, b) === -sortFn(b, a)

If you return only values of 0 and 1, you miss -1.

console.log([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sort(function(a, b) {
    console.log(a, b, a > b);
    return a > b;
}));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392