如果我有一个数组:
myArray = [['0','Mouse'],['1','Dog'],['2','Cat'],['3','Gerbil']];
如何根据动物(在本例中)对数组进行字母顺序排列?
myArray = alpha(myArray);
结果:
myArray = [['2','Cat'],['1','Dog'],['3','Gerbil'],['0','Mouse']];
最佳答案
您可以使用排序
功能
var myArray = [['0','Mouse'],['1','Dog'],['2','Cat'],['3','Gerbil']];
console.log(alpha(myArray));
var arr2 = [['5','Mouse'],['0','Mouse'],['1','Dog'],['2','Cat'],['3','Gerbil']];
console.log('another array', alpha(arr2));
function alpha(arr) {
return arr.sort((a, b) => a[1] > b[1]);
}
关于javascript - 使用 jquery 或 javascript 按字母顺序排列多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41493090/