我想对这条记录进行排序,我的输入数组是 a1
var a1 = ["ap1","ap10","ap11","ap2","ap3","ap4","ap5","ap6","ap7","ap8","ap9"];
输出应该是这样的:
["ap1","ap2","ap3","ap4","ap5","ap6","ap7","ap8","ap9","ap10","ap11"];
这是代码,
var a1 = ["ap1","ap10","ap11","ap2","ap3","ap4","ap5","ap6","ap7","ap8","ap9"];
var a2 = a1.sort(function(a,b){
var charPart = [a.substring(0,1), b.substring(0,1)],
numPart = [a.substring(1)*1, b.substring(1)*1];
if(charPart[0] < charPart[1]) return -1;
else if(charPart[0] > charPart[1]) return 1;
else{ //(charPart[0] == charPart[1]){
if(numPart[0] < numPart[1]) return -1;
else if(numPart[0] > numPart[1]) return 1;
return 0;
}
});
alert(a2.toString());