I have a alphanumeric array -
var a1 =["3800tl-24G-2XG-PoEP", "3500", "stack-TA", "2620-48-PoEP", "2120", "3800tl-24G-2XG-PoEP"];
I tried sorting this array using natsort which gives me the correct output -
`Array ( [4] => 2120 [3] => 2620-48-PoEP [1] => 3500 [0] => 3800tl-24G-2XG-PoEP [5] => 3800tl-24G-2XG-PoEP [2] => stack-TA )`
I tried doing the same with javascript -
var a1 =["3800tl-24G-2XG-PoEP", "3500", "stack-TA", "2620-48-PoEP", "2120", "3800tl-24G-2XG-PoEP"];
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;
}
});
$('#r').html(a2.toString())
which gives me the output -
2620-48-PoEP,2120,3800tl-24G-2XG-PoEP,3500,3800tl-24G-2XG-PoEP,stack-TA.
Why the javascript code is not taking 2120 as the 1st element. What is wrong with the javascript code ?
[], instead ofsubstring(), e.g.:var charPart = [a[0], b[0]];