我有一个像这样的数组
[
["Adrian Garcia", "42"],
["Alberto Guglielmi", "42"],
["Alice Fung Yee Ng", "15"],
["Christopher Wilson", "4"],
["Claudio Meneghetti", "13"],
["Bahadir Tanriover", "15"],
["Baitinger Martin", "36"],
["Bill Cash", "15"],
["Brian Kuhlmann", "15"],
["Caesar Lima", "15"],
["Carl Tremblay", "42"],
["Aorta", "42"],
["Charles Harris", "15"],
["Chet Morrison", "15"],
["Chico Audi", "11"],
["Andreas Gemperle", "42"],
["Angel Burns", "42"],
["Arno Bosma", "42"],
["Chris Bailey", "15"],
["Chris Close", "1"],
["Christoph Martin Schmid", "42"],
["Ben Isselstein", "25"],
["Colin Thomas", "15"],
["Craig Cameron Olsen", "42"],
["Craig Easton", "42"]
]
我需要按数字排序此数组,并按名称升序排序。我找到了大多数按单值排序的解决方案。但我需要按两个值排序。
我需要输出如下。
[
["Chris Close", "1"]
["Christopher Wilson", "4"]
["Chico Audi", "11"]
["Claudio Meneghetti", "13"]
["Alice Fung Yee Ng", "15"]
["Bahadir Tanriover", "15"]
["Bill Cash", "15"]
["Brian Kuhlmann", "15"]
["Caesar Lima", "15"]
["Charles Harris", "15"]
["Chet Morrison", "15"]
["Chris Bailey", "15"]
["Colin Thomas", "15"]
["Ben Isselstein", "25"]
["Baitinger Martin", "36"]
["Adrian Garcia", "42"]
["Alberto Guglielmi", "42"]
["Carl Tremblay", "42"]
["Aorta", "42"]
["Andreas Gemperle", "42"]
["Angel Burns", "42"]
["Arno Bosma", "42"]
["Christoph Martin Schmid", "42"]
["Craig Cameron Olsen", "42"]
["Craig Easton", "42"]
]
答案 0 :(得分:2)
尝试这样的事情:
$(array).sort(function(first, second){
if(first[0] < second[0]){return -1;}
else if(first[0] > second[0]){return 1;}
else if(first[0] === second[0]){
if(parseInt(first[1]) < parseInt(second[1])){return -1;}
else return 1;
}
});
如果你想先按号码排序,请使用:
$(array).sort(function(first, second){
if(parseInt(first[1]) < parseInt(second[1])){return -1;}
else if(parseInt(first[1]) > parseInt(second[1])){return 1;}
else if(parseInt(first[1]) === parseInt(second[1])){
if(first[0] < second[0]){return -1;}
else return 1;
}
});
答案 1 :(得分:1)
较短的版本只是使用JavaScripts自己的sort
函数:
// First sort on number (second column)
array.sort(function(a, b) { return a[1] - b[1] });
// Then sort on name (first column)
array.sort(function(a, b) { return a[0] - b[0] });
以
输出[
["Chris Close", "1"],
["Christopher Wilson", "4"],
["Chico Audi", "11"],
["Claudio Meneghetti", "13"],
["Alice Fung Yee Ng", "15"],
["Bahadir Tanriover", "15"],
["Bill Cash", "15"],
["Brian Kuhlmann", "15"],
["Caesar Lima", "15"],
["Charles Harris", "15"],
["Chet Morrison", "15"],
["Chris Bailey", "15"],
["Colin Thomas", "15"],
["Ben Isselstein", "25"],
["Baitinger Martin", "36"],
["Adrian Garcia", "42"],
["Alberto Guglielmi", "42"],
["Carl Tremblay", "42"],
["Aorta", "42"],
["Andreas Gemperle", "42"],
["Angel Burns", "42"],
["Arno Bosma", "42"],
["Christoph Martin Schmid", "42"],
["Craig Cameron Olsen", "42"],
["Craig Easton", "42"]
]
答案 2 :(得分:-1)
我按数字排序
var arr=[
["Adrian Garcia", 42],
["Alberto Guglielmi", 42],
["Alice Fung Yee Ng", 15],
["Christopher Wilson",4],
["Claudio Meneghetti", 13],
["Bahadir Tanriover", 15],
["Baitinger Martin", 36],
["Bill Cash", 15],
["Brian Kuhlmann", 15],
["Caesar Lima", 15],
["Carl Tremblay", 42],
["Aorta", 42],
["Charles Harris", 15],
["Chet Morrison", 15],
["Chico Audi",11],
["Andreas Gemperle", 42],
["Angel Burns", 42],
["Arno Bosma", 42],
["Chris Bailey", 15],
["Chris Close", 1],
["Christoph Martin Schmid", 42],
["Ben Isselstein", 25],
["Colin Thomas", 15],
["Craig Cameron Olsen", 42],
["Craig Easton", 42]
]
for (var i = 0; i < arr.length; i++) {// No of times loop executed for given array
for (var j = 0; j < (arr.length - i) - 1; j++) { // it wil save the one value each iteration
if (arr[j][1] > arr[j + 1][1]) {
var temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
alert(arr);