1

I was under the impression that in order to sort an array of numbers you had to do the following:

var numbers = [4, 1, 2, 3];

function compare(a, b) {
    return a - b;
} 

numbers.sort(compare);

However, I found that the following seems to work:

var numbers = [5, 3, 2, 6, 4, 7];
var sortNumbers = numbers.sort();
console.log(sortNumbers); 

Is there anything wrong with doing it this way? It seems to work in all cases for me.

CTK
  • 91
  • 2
  • 8
  • 1
    You should use the first snippet you posted. Look at this: `[11, 2, 10, 1, 5].sort()` -> `[1, 10, 11, 2, 5]`. – Josh Crozier Dec 07 '15 at 21:40
  • A compare function gives you more control over how a sort is executed. It's especially useful when dealing with arrays of objects that cannot be compared directly. The compare function must return a negative or positive number if one item comes before another, or zero if the two items being compared should be considered equal in terms of the sort algorithm. – Thriggle Dec 07 '15 at 21:43
  • it's funny. I ended up here because I read the title question and thought, "no, there are [several non-comparative numeric sorting algorithms to choose from](http://pages.cs.wisc.edu/~paton/readings/Old/fall01/LINEAR-SORTS.html)". Supplying a comparison callback was not on my mind. – kojiro Dec 07 '15 at 21:59

2 Answers2

2

It seems to work in all cases for me.

You have not considered any cases of numbers that consist of multiple digits in string representation. The lexicographic default comparison also fails on negative numbers.

See How to sort an array of integers correctly for more information and some counterexamples.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 2
    This answer could be improved by describing _why_ multiple digits in string representation produces unexpected results. – kojiro Dec 07 '15 at 21:43
  • You could add this example: var scores = [1, 10, 2, 21]; scores.sort(); // [1, 10, 2, 21] // Watch out that 10 comes before 2, // because '10' comes before '2' in Unicode code point order. – CoderPi Dec 07 '15 at 21:44
  • @kojiro: I would have hoped the linked question explains everything. – Bergi Dec 07 '15 at 21:47
  • 1
    @Bergi It does, but links rot (even on StackExchange). That's why there are [conventions against link-only answers](http://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer). Your answer is more than a link-only answer, but IMO somewhat less than a complete answer. – kojiro Dec 07 '15 at 21:50
  • @kojiro: I see (and have included a sub-clause), but I don't think this StackExchange link will rot. Yes, I could write a complete article about the topic, but I won't do it here. I had considered only commenting with that link, but actually the concrete question ("*What did I miss?*") is easily answered. – Bergi Dec 07 '15 at 21:56
0

There is a default sort function as stated here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

The default sort order is according to string Unicode code points.

var scores = [1, 10, 2, 21]; 
scores.sort(); // [1, 10, 2, 21]
// Watch out that 10 comes before 2,
// because '10' comes before '2' in Unicode code point order.

So yes, it will not sort the numbers as you'd like. It will sort them as strings.

For easy usage you can do the sort function inline:

numbers.sort(function(a,b){return a - b});
CoderPi
  • 12,985
  • 4
  • 34
  • 62