How does the Sort method work?

Asked

Viewed 791 times

6

I’m starting to learn javascript and while searching for examples of Sort I came across the following code:

var numeros = [1,2,3,4,5,6,7,8,9,10];
numeros.sort(function (a, b) {
   return (a % 2 !=0);
});

console.log (numeros);

The console output looks like this:

(10) [2,4,6,8,10,9,7,5,3,1]

I just wanted to understand what Sort did to get this result.

2 answers

6


The function .sort should always return numbers. Negative, positive or zero. That is, this function is badly drawn.

To specification is clear:

Returns to Negative value if x < y, zero if x = y, or a Positive value if x > y

However, and without guaranteeing that this behavior will be the same in all browsers (since it does not give the expected return), what happens is:

Imagine this Sort:

[true, false, false, true, true].sort((a, b) => a != b)

the result is

[false, false, true, true, true]

In the case of a % 2 !=0 which is basically a condition that gives true if the number is odd .sort will give priority to the results that give false thus solved the beginning of the result 2,4,6,8,10 and then the rest.

If we put together a console.log to understand why the odd ones are reversed:

var numeros = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
numeros.sort(function(a, b) {
  console.log(b, '>', numeros.join(','));
  return (a % 2 != 0);
});

that is, as the array is iterated the odd number of index N is in second place, and in the next iteration this previous odd index N is pushed right by the odd noo.

  • 1

    Only by strengthening Sergio’s answer: it is not possible to order stone, paper and scissors. Computational ordering requires a > b > c, necessarily a > c. But that doesn’t apply to stone, paper and scissors, where pedra > tesoura > papel > pedra

5

The function sort of Array sorts the array based on the function passed. The documentation says that if in the function the value returned is less than 0 then the value a goes to the beginning, otherwise a goes to the end.

In your case you happen to be testing whether a is odd with:

return (a % 2 !=0);

That will give false if it is even and true if it is odd, it is placing the pairs first. To respect the expected numerical return for the function sort can turn you into:

return (a % 2);

Which will already return a positive number if it is odd or 0 if you are even:

var numeros = [1,2,3,4,5,6,7,8,9,10];
numeros.sort(function (a, b) {
   return (a % 2);
});

console.log(numeros);

This function is however more useful for comparing objects, where it can indicate the comparison rules of these, which in turn force the ordering:

const pessoas = [
  { nome : "Claudio", idade : 22 },
  { nome : "Marta", idade : 19 },
  { nome : "Roberto", idade : 38 },
  { nome : "Jessica", idade : 27 }
];

let pessoasPorNome = pessoas.sort(function(a,b){
  return a.nome > b.nome;
});

console.log("Pessoas por nome:",pessoasPorNome);

let pessoasPorIdade = pessoas.sort(function(a,b){
  return a.idade > b.idade;
});

console.log("Pessoas por idade:",pessoasPorIdade);

Documentation for the Sort function

Browser other questions tagged

You are not signed in. Login or sign up in order to post.