-2

I use sort() to sort my table but I do not understand why it does not work. you have an idea ?

var tab = [5, 15, 17, 3, 8, 11, 28, 6, 55, 7];
tab = tab.sort();

for (var i = 0; i < tab.length; i++) {
  $("p").append(" ", tab[i]);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>

https://jsfiddle.net/1423tLwd/

Nope
  • 22,147
  • 7
  • 47
  • 72
DenisMasot
  • 697
  • 5
  • 11
  • 21
  • This question doesn't work? Do you have any idea why? Btw, it looks like the array is sorted according to the standards. – Teemu Sep 27 '17 at 10:14
  • 2
    This must be a dup somewhere `tab.sort( function(a,b){ return a-b })` – gurvinder372 Sep 27 '17 at 10:14
  • 2
    `The default sort order is according to string Unicode code points.` – Keith Sep 27 '17 at 10:16
  • 2
    Possible duplicate of [**Javascript sort() not alphabetizing properly**](https://stackoverflow.com/questions/14571140/javascript-sort-not-alphabetizing-properly) or even better [**How does sort function work in JavaScript, along with compare function**](https://stackoverflow.com/questions/6567941/how-does-sort-function-work-in-javascript-along-with-compare-function) – Nope Sep 27 '17 at 10:16
  • 2
    It does work, the numbers are sorted alphabetically. – RemcoGerlich Sep 27 '17 at 10:17
  • The answer is in tons of SO posts and searching google for your exact title gives the answer as well how to use `sort` In addition please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Nope Sep 27 '17 at 10:18
  • "_Questions seeking debugging help ("why isn't this code working?") __must include the desired behavior, a specific problem__ or error and the shortest code necessary to reproduce it in the question itself. __Questions without a clear problem statement are not useful to other readers__. See: How to create a [mcve]._" – Teemu Sep 27 '17 at 10:22
  • @Fran Not a dup, we even don't know, what is wrong with the code. It's pure speculation to give a number order as an answer, it might be, that OP wants a reversed order, or what ever ... – Teemu Sep 27 '17 at 10:24
  • @Teemu I doubt it! SO posts like this https://stackoverflow.com/questions/6567941/how-does-sort-function-work-in-javascript-along-with-compare-function and the actual documentation for it explain it well enough that one should be able to sort in any order preferred. – Nope Sep 27 '17 at 10:25

2 Answers2

2

By default the sort method will sort the array that it has been called on alphabetically.

To get around this you need to pass sort a callback function that will sort the elements by their numerical value.

To achieve this you need to do the following:

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

let tab = [5, 15, 17, 3, 8, 11, 28, 6, 55, 7];
let sortedTab = tab.sort(sortNumber);
console.log(sortedTab);
Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54
-1

As Explained in MDN web docs:

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

That is, you should give sort the function that is to compare elements of the array, otherwise, your array will be sorted according to string Unicode code points.

This should work (sort in ascending order):

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

// sort tab array using compareFunction to compare elements
tab.sort(compareFunction);
Ammar
  • 770
  • 4
  • 11