我想排序包含數字& alpha序列的字符串列表。我想先按數字排序,然後按alpha,如1a,2b,4,77c,743等排序。排序numAlpha字符串列表
我一直在玩着下面的javascript,我已經接近將我的頭撞過壁。任何人都可以給我一些指點嗎?
var a1 = ["4a", "4c", "1a", "4b", "13c", "4a", "1a", "11c", "112c", "12", "9"];
var a2 = a1.sort(function(a, b) {
if (a.slice(-1) == "a" || a.slice(-1) == "b" || a.slice(-1) == "c") {
var charPart = [a.slice(-1), b],
numPart = [a.slice(0, a.length - 1), b];
if (b.slice(-1) == "a" || b.slice(-1) == "b" || b.slice(-1) == "a") {
var charPart = [a.slice(-1), b.slice(-1)],
numPart = [a.slice(0, a.length - 1), b.slice(0, b.length - 1)];
}
}
if (numPart[0] < numPart[1]) return -1;
else if (numPart[0] > numPart[1]) return 1;
else if (numPart[0] == numPart[1]) return -1;
else {
if (charPart[0] < charPart[1]) return -1;
else if (charPart[0] > charPart[1]) return 1
}
});
http://jsfiddle.net/8fRsD/307/
「a2」中你想要的結果是什麼? –