我有一个我需要排序的数组 -
var array1 = ["AAA","BB","B+","AA+","CC","AA-","B","A","AA"];
在应用过滤器/排序后,我想要像 -
这样的东西var array1 = ["AAA","AA+","AA","AA-","A","BB","B+","B","CC"];
此处字母表的优先级高于运营商,如在评选AAA之后应该在AA +
之前我该怎么做?
答案 0 :(得分:2)
如上所述,您需要传递自定义比较功能进行排序。像这样的东西
function s(a,b){
function normalize(str){
return str.concat(new Array(4-str.length).join(';')).replace(/[A-Z+-]/g, function($1){
return replacer[$1] ? replacer[$1] : $1;
});
}
var replacer = {
'A' : '1',
'B' : '2',
'C' : '3',
'+' : ':',
'-' : '<'
},
ar = normalize(a),
br = normalize(b);
return ar > br ? 1
: ar < br ? -1
: 0;
}
var array1 = ["AAA","BB","B+","AA+","CC","AA-","B","A","AA"];
function s(a,b){
function normalize(str){
return str.concat(new Array(4-str.length).join(';')).replace(/[A-Z+-]/g, function($1){
return replacer[$1] ? replacer[$1] : $1;
});
}
var replacer = {
'A' : '1',
'B' : '2',
'C' : '3',
'+' : ':',
'-' : '<'
},
ar = normalize(a),
br = normalize(b);
return ar > br ? 1
: ar < br ? -1
: 0;
}
document.getElementById('before').innerHTML = 'unsorted: ' + array1.join();
array1.sort(s);
document.getElementById('result').innerHTML = 'sorted: ' + array1.join();
<span id="before"></span><br>
<span id="result"></span>
更新:更通用
function sorting(maxLen) {
return function s(a, b) {
function checkLength(str, maxlen) {
if (str.length > maxlen) {
throw new Error('string: "' + str + '" (' + str.length + ') too long, max len: ' + maxlen);
}
}
function normalize(str, maxlen) {
return str.concat(new Array(maxlen + 1 - str.length).join(';')).replace(/[A-Z+-]/g, function($1) {
return replacer[$1] ? replacer[$1] : $1;
});
}
checkLength(a, maxLen);
checkLength(b, maxLen);
var replacer = {
'A': '1',
'B': '2',
'C': '3',
'+': ':',
'-': '<'
},
ar = normalize(a, maxLen),
br = normalize(b, maxLen);
return ar > br ? 1 : ar < br ? -1 : 0;
}
}
并使用它的功能
array1.sort(sorting(array1.reduce(function(a,b){return Math.max(a, b.length);},0)))
var array1 = ["AAAA", "BB", "BBBB+", "AAA+", "CC", "AA-", "BBBB", "A", "AA"];
var array2 = ["AAAA", "BB", "BBBBB+", "AAA+", "CC", "AA-", "BBBB", "A", "AA"];
function sorting(maxLen) {
return function s(a, b) {
function checkLength(str, maxlen) {
if (str.length > maxlen) {
throw new Error('string: "' + str + '" (' + str.length + ') too long, max len: ' + maxlen);
}
}
function normalize(str, maxlen) {
return str.concat(new Array(maxlen + 1 - str.length).join(';')).replace(/[A-Z+-]/g, function($1) {
return replacer[$1] ? replacer[$1] : $1;
});
}
checkLength(a, maxLen);
checkLength(b, maxLen);
var replacer = {
'A': '1',
'B': '2',
'C': '3',
'+': ':',
'-': '<'
},
ar = normalize(a, maxLen),
br = normalize(b, maxLen);
return ar > br ? 1 : ar < br ? -1 : 0;
}
}
function test(witherror, arr, maxlen) {
document.getElementById(witherror + 'before').innerHTML = 'unsorted: ' + arr.join();
try {
arr.sort(sorting(maxlen||arr.reduce(function(a,b){return Math.max(a, b.length);},0)));
document.getElementById(witherror + 'result').innerHTML = 'sorted: ' + arr.join();
} catch (e) {
document.getElementById(witherror + 'result').innerHTML = e;
}
}
test('', array1,5);
test('e', array2,5);
test('a', array2);
<span>sample with error, string is too long</span>
<br>
<span id="ebefore"></span>
<br>
<span id="eresult"></span>
<hr>
<span>sample without error, maxlen = 5</span>
<br>
<span id="before"></span>
<br>
<span id="result"></span>
<hr>
<span>sample without error</span>
<br>
<span id="abefore"></span>
<br>
<span id="aresult"></span>
答案 1 :(得分:0)
您需要定义自定义比较功能并将其传递给排序
array.sort(myfunc)
该函数应返回负值,零值或正值,具体取决于参数。 下面的示例函数
myfunc = function(a, b){
//alphabet to be compared
a1 = a.replace(/^[A-Z]+/,'');
b1 = b.replace(/^[A-Z]+/,'');
// in cases where alphabet are same , compare the full string.
if(a1==b1 ){
a1 = a;
b1 = b;
}
if ( a1 < b1 ) return -1;
if ( a1 > b1 ) return 1;
return 0;
}
答案 2 :(得分:0)
请尝试以下自定义功能:
array1.sort(mySort);
function mySort(a,b){
var A = a;
var B = b;
if (A < B) {
return -1;
}
else if (A > B) {
return 1;
}
else {
return 0;
}
}
答案 3 :(得分:-1)