Sort Mixed Alpha/numeric Array
Solution 1:
var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;
functionsortAlphaNum(a, b) {
var aA = a.replace(reA, "");
var bA = b.replace(reA, "");
if (aA === bA) {
var aN = parseInt(a.replace(reN, ""), 10);
var bN = parseInt(b.replace(reN, ""), 10);
return aN === bN ? 0 : aN > bN ? 1 : -1;
} else {
return aA > bA ? 1 : -1;
}
}
console.log(
["A1", "A10", "A11", "A12", "A2", "A3", "A4", "B10", "B2", "F1", "F12", "F3"].sort(sortAlphaNum)
)
Solution 2:
constsortAlphaNum = (a, b) => a.localeCompare(b, 'en', { numeric: true })
Usage:
constsortAlphaNum = (a, b) => a.localeCompare(b, 'en', { numeric: true })
console.log(['A1', 'A10', 'A11', 'A12', 'A2', 'A3', 'A4', 'B10', 'B2', 'F1', 'F12', 'F3'].sort(sortAlphaNum))
Gives:
["A1", "A2", "A3", "A4", "A10", "A11", "A12", "B2", "B10", "F1", "F3", "F12"]
You may have to change the 'en'
argument to your locale or determine programatically but this works for english strings.
localeCompare
is supported by IE11, Chrome, Firefox, Edge and Safari 10.
Solution 3:
I had a similar situation, but, had a mix of alphanumeric & numeric and needed to sort all numeric first followed by alphanumeric, so:
A10
1
5
A9
2
B3
A2
needed to become:
1
2
5
A2
A9
A10
B3
I was able to use the supplied algorithm and hack a bit more onto it to accomplish this:
var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;
functionsortAlphaNum(a,b) {
var AInt = parseInt(a, 10);
var BInt = parseInt(b, 10);
if(isNaN(AInt) && isNaN(BInt)){
var aA = a.replace(reA, "");
var bA = b.replace(reA, "");
if(aA === bA) {
var aN = parseInt(a.replace(reN, ""), 10);
var bN = parseInt(b.replace(reN, ""), 10);
return aN === bN ? 0 : aN > bN ? 1 : -1;
} else {
return aA > bA ? 1 : -1;
}
}elseif(isNaN(AInt)){//A is not an Intreturn1;//to make alphanumeric sort first return -1 here
}elseif(isNaN(BInt)){//B is not an Intreturn -1;//to make alphanumeric sort first return 1 here
}else{
return AInt > BInt ? 1 : -1;
}
}
var newlist = ["A1", 1, "A10", "A11", "A12", 5, 3, 10, 2, "A2", "A3", "A4", "B10", "B2", "F1", "F12", "F3"].sort(sortAlphaNum);
Solution 4:
A simple way to do this is use the localeCompare() method of JavaScript
https://www.w3schools.com/jsref/jsref_localecompare.asp
Example:
exportconstsortAlphaNumeric = (a, b) => {
// convert to strings and force lowercase
a = typeof a === 'string' ? a.toLowerCase() : a.toString();
b = typeof b === 'string' ? b.toLowerCase() : b.toString();
return a.localeCompare(b);
};
Expected behavior:
1000XRadoniusMaximus10XRadonius200XRadonius20XRadonius20XRadoniusPrime30XRadonius40XRadoniusAllegia50ClasteronAllegia500ClasteronAllegia50BClasteronAllegia51ClasteronAllegia6RClasteronAlpha100Alpha2Alpha200Alpha2AAlpha2A-8000Alpha2A-900CallistoMorphamaxCallistoMorphamax500CallistoMorphamax5000CallistoMorphamax600CallistoMorphamax6000 SECallistoMorphamax6000 SE2CallistoMorphamax700CallistoMorphamax7000XiphXlater10000XiphXlater2000XiphXlater300XiphXlater40XiphXlater5XiphXlater50XiphXlater500XiphXlater5000XiphXlater58
Solution 5:
You can use Intl.Collator
It has performance benefits over localeCompare
Read here
Browser comparability ( All the browser supports it )
let arr = ["A1", "A10", "A11", "A12", "A2", "A3", "A4", "B10", "B2", "F1", "F12", "F3"]
let op = arr.sort(newIntl.Collator('en',{numeric:true, sensitivity:'accent'}).compare)
console.log(op)
Post a Comment for "Sort Mixed Alpha/numeric Array"