0

I want to sort an array of numerical values and I find a code likes :

<!DOCTYPE html>
<html>
<body>

<p>Click the button to sort the array in ascending order.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;    

function myFunction() {
    points.sort(function(a, b){return a-b});
    document.getElementById("demo").innerHTML = points;
}
</script>

</body>
</html>

Output:

1,5,10,25,40,100

The code runs properly but I can't understand how the statement points.sort(function(a, b){return a-b});is working and what is a and b?

Aditya
  • 1,214
  • 7
  • 19
  • 29
  • 1
    [Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) – lastr2d2 Aug 07 '14 at 05:43
  • 2
    why don't you do a `console.log` & see what are the values of a & b. – Mritunjay Aug 07 '14 at 05:44
  • 2
    this question's answers is enough for your doubts [How to sort number in javascript sort method](http://stackoverflow.com/questions/4576903/how-to-sort-number-in-javascript-sort-method) – Govind Singh Aug 07 '14 at 05:45
  • @lastr2d2 pointed you to the documentation. The important thing to note there is that the `sort` method compares again `0`, `greater than 0` and `less than 0`. While returning the specific values of `0, 1, -1` are common they aren't required. – Jeremy J Starcher Aug 07 '14 at 05:52

3 Answers3

1

Array.sort is a function, which takes compare function as a parameter.

When sorting, sort function calls that compare function, passing in two elements of an array being sorted.

  • If this function returns value, which is less than zero, than first element should stay left from the second.

  • If it returns number, which is greater than zero, than first element should stay right from the second (in sorted array)

  • If it returns zero, than these elements are equal and it doesn't matter in which order they stay.

So to change the order of sort (if you need), you can just switch the compare function to

return b - a;
Vyacheslav Pukhanov
  • 1,788
  • 1
  • 18
  • 29
0

This would be easy to understand:

if(a < b){
  return -1;
} else if( a > b){
  return 1;
} else { 
  return 0;
}
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

a and b are numbers in the array which are compared with each other,

Hence if a-b comes out to be negative then it signifies that b>a so if the list is ascending it will put a ahead of b

As per Mozilla Docs

If compareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.

If compareFunction(a, b) is greater than 0, sort b to a lower index than a. compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments.

If inconsistent results are returned then the sort order is undefined

Community
  • 1
  • 1
V31
  • 7,626
  • 3
  • 26
  • 44