1var reA = /[^a-zA-Z]/g;
2var reN = /[^0-9]/g;
3
4function sortAlphaNum(a, b) {
5 var aA = a.replace(reA, "");
6 var bA = b.replace(reA, "");
7 if (aA === bA) {
8 var aN = parseInt(a.replace(reN, ""), 10);
9 var bN = parseInt(b.replace(reN, ""), 10);
10 return aN === bN ? 0 : aN > bN ? 1 : -1;
11 } else {
12 return aA > bA ? 1 : -1;
13 }
14}
15console.log(
16["A1", "A10", "A11", "A12", "A2", "A3", "A4", "B10", "B2", "F1", "F12", "F3"].sort(sortAlphaNum)
17)
1const sortAlphaNum = (a, b) => a.localeCompare(b, 'en', { numeric: true })
2console.log(['A1', 'A10', 'A11', 'A12', 'A2', 'A3', 'A4', 'B10', 'B2', 'F1', 'F12', 'F3'].sort(sortAlphaNum))