TransWikia.com

Why does points.sort(function(a, b){return a-b}); return -1, 0 or 1?

Stack Overflow Asked by Camarellini Viberg on November 4, 2021

My difficulty here could be my mathematical illiteracy, but I was trying to sort some numbers in a JavaScript array and this is the solution I found online. It does indeed work, but my question is why?! I would really like to understand this piece of code properly.

The site, W3 Schools says:

You can fix this by providing a function that returns -1, 0, or 1:

var points = [40, 100, 1, 5, 25, 10];

points.sort(function(a,b){return a-b});

Why would only -1, 0 or 1 be returned? I have Googled, and return can return pretty much any value you want.

Again, if this is an incredibly dumb question I apologise.

6 Answers

I am trying to answer your "It does indeed work BUT Why?"

Here is the syntax of the sort method in javascript. Note that the compareFunction is optional.

array.sort([compareFunction]);

When compareFunction is not provided:

When compareFunction is not provided, the default mode of sorting is followed wherein the elements of the array are converted into strings and than compared according to the UTF 16 code unit values and the elements are sorted in ascending order by default.

e.g. For string values, it follows simple UTF 16 code order. like "Banana" appears before "Cat"

for numbers, they first gets converted into strings and then compared according to UTF 16 code oder. So, 80 and 9 gets converted to strings "80" and "9" respectively and "80" comes before "9" according to the UTF 16 order.

When compareFunction is provided:

When compareFunction is provided, then the function takes over the sorting process and all the non-undefined elements gets sorted based on the return value of compareFunction according to the following logic.

If the compareFunction(a,b) returns negative value, a comes first by getting lower index than b.

If compareFunction(1,b) returns zero then both get the same index and hence do not change place respect to each other but both gets sorted with all other elements.

If compareFunction(a,b) returns positive value, a gets higher index than b and hence a comes after b.

The example you provided, uses a trick to decide each numbers comparative weightage in terms of its numeric value by subtracting each pair of numbers from the array.

Answered by Yogi Agravat on November 4, 2021

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

If Function(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 Function(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first. Function(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.

Answered by Mitul Panchal on November 4, 2021

It is not bound to be -1, 0, 1 any negative or positive number will do the trick.

But how it works? The sort() method needs to know the relation between each two elements in order to sort them. For each pair (according to the algorithm used) it calls the function then based on the return value, may swap them. Note that a-b returns a negative value if b>a and a positive one if a>b. That leads to an ascending order.

Just for test, replace a-b with b-a and you will get a descending order. You may even make it more complicated, sort them based on (for example) their least significant digit:

a.sort(function() {return (a%10) - (b%10);}

Answered by Alireza on November 4, 2021

Quoting from ECMA Script Reference:

Array.prototype.sort (comparefn)

The elements of this array are sorted. The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order). If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative value if x < y, zero if x = y, or a positive value if x > y.

The rest are implementation details that could differ between interpreters.

Answered by aa333 on November 4, 2021

The sort callback has to return

  • a negative number if a < b
  • 0 if a === b
  • a positive number if a > b

Three possible return values are needed because the sort function needs to know whether a is smaller than, equal to, or larger than b in order to correctly position a in the result array.

It is very common to just return -1, 0 and 1 if you working with non-numerical data (I guess that's why W3Schools mentions it). But if you use numerical data, you can simply subtract the values because

  • if a < b then a - b < 0, i.e. a negative number
  • if a === b then a - b === 0, i.e. 0
  • if a > b then a - b > 0, i.e. a positive number

W3Schools is not very precise which is one of the reason why you should avoid it. Use MDN instead.

Answered by Felix Kling on November 4, 2021

See the specification, http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4.14

Don't rely on things that w3schools has to say.

If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative value if x < y, zero if x = y, or a positive value if x > y.

Answered by Xotic750 on November 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP