Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array in ascending order based on ints and not strings

I have an array with this structure:

myArray = [ [<number>, [<string>] ], [<number>, [<string>] ], ... ];

I'd like to sort the array according to the ints. Unfortunately, when I call .sort() on myArray it returns me an array sorted according to the strings. How could I solve this?

like image 577
Federico Capello Avatar asked Apr 29 '13 13:04

Federico Capello


1 Answers

Try this

myArray.sort(function(a,b) {return a[0]-b[0]})
like image 188
Oktav Avatar answered Sep 25 '22 16:09

Oktav