sorting 2d array js by string
var newInv = [
[2, "Hair Pin"],
[3, "Half-Eaten Apple"],
[67, "Bowling Ball"],
[7, "Toothpaste"]
];
console.log(newInv);
newInv = newInv.sort(function(a,b) {
if (a[1] < b[1]) return -1;
if (a[1] > b[1]) return 1;
return 0;
});
console.log(newInv);
[2, "Hair Pin"],
[3, "Half-Eaten Apple"],
[67, "Bowling Ball"],
[7, "Toothpaste"]
];
console.log(newInv);
newInv = newInv.sort(function(a,b) {
if (a[1] < b[1]) return -1;
if (a[1] > b[1]) return 1;
return 0;
});
console.log(newInv);
Comments
Post a Comment