I have the following array:
I'd like to sort the same for
id - 15135
id - 50
id - 25
I tried to use
array.sort
But it did not work.
var meuArray = [['4togm90gjwegn', 50], ['ef84itjdpodsdf4jg', 25], ['32r8uoijgtsdfsht', 15135]];
//Para ordenar em ordem crescente
meuArray.sort((a, b) => a[1] - b[1]);
//Para ordenar em ordem decrescente
meuArray.sort((a, b) => b[1] - a[1]);
The sort method, if called without any parameters, attempts to sort its array by comparing string values, but when its array is not made up of strings, you can pass a callback function with instructions to the organization.
This function takes 2 arguments, the first and the next array item (a and b), so it is up to you to return a number greater than 0 to say that element a precedes b, or less than 0 to say that b precedes a . Because the values compared are numeric, in that case you can just subtract them.