var array = [[1, "grape", 42], [2, "fruit", 9]];
array.sort(function(a, b)
{
// a and b will here be two objects from the array
// thus a[1] and b[1] will equal the names
// if they are equal, return 0 (no sorting)
if (a[1] == b[1]) { return 0; }
if (a[1] > b[1])
{
// if a should come after b, return 1
return 1;
}
else
{
// if b should come after a, return -1
return -1;
}
});
The sort
function takes an additional argument, a function that takes two arguments. This function should return -1
, 0
or 1
depending on which of the two arguments should come first in the sorting. More info.
I also fixed a syntax error in your multidimensional array.