このレコードをソートしたいのですが、入力配列は 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());