Organizational Research By

Surprising Reserch Topic

how to sorting in javascript shouldnt return a b be enough for a comparison funct


sorting in javascript shouldnt return a b be enough for a comparison funct  using -'javascript,sorting,comparison'

I have always successfully sorted my arrays like this (when I did not want the standard lexicographic ordering):

var arr = […] // some numbers or so
arr.sort(function(a, b) {
    return a > b;
});

Now, someone told me this was wrong, and that I would need to return a-b instead. Is that true, and if yes why? I have tested my comparison function, and it works! Also, why would my solution be so common when it is wrong?
asked Sep 8, 2015 by rajesh
edited Sep 9, 2015 by rajesh
0 votes
35 views



Related Hot Questions



Government Jobs Opening


...