我有一个混合数组,我需要按字母然后按数字排序
[A1, A10, A11, A12, A2, A3, A4, B10, B2, F1, F12, F3]
我如何将其排序为:
[A1, A2, A3, A4, A10, A11, A12, B2, B10, F1, F3, F12]
我努力了
arr.sort(function(a,b) {return a - b});
但这只是按字母顺序排序。这可以用直接的 JavaScript 或 jQuery 来完成吗?
我有一个混合数组,我需要按字母然后按数字排序
[A1, A10, A11, A12, A2, A3, A4, B10, B2, F1, F12, F3]
我如何将其排序为:
[A1, A2, A3, A4, A10, A11, A12, B2, B10, F1, F3, F12]
我努力了
arr.sort(function(a,b) {return a - b});
但这只是按字母顺序排序。这可以用直接的 JavaScript 或 jQuery 来完成吗?
var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;
function sortAlphaNum(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)
)
const sortAlphaNum = (a, b) => a.localeCompare(b, 'en', { numeric: true })
用法:
const sortAlphaNum = (a, b) => a.localeCompare(b, 'en', { numeric: true })
console.log(['A1', 'A10', 'A11', 'A12', 'A2', 'A3', 'A4', 'B10', 'B2', 'F1', 'F12', 'F3'].sort(sortAlphaNum))
给出:
["A1", "A2", "A3", "A4", "A10", "A11", "A12", "B2", "B10", "F1", "F3", "F12"]
您可能需要将'en'
参数更改为您的语言环境或以编程方式确定,但这适用于英文字符串。
localeCompare
IE11、Chrome、Firefox、Edge 和 Safari 10 支持。
我有类似的情况,但是,混合了字母数字和数字,需要先对所有数字进行排序,然后是字母数字,所以:
A10
1
5
A9
2
B3
A2
需要成为:
1
2
5
A2
A9
A10
B3
我能够使用提供的算法并对其进行更多修改以完成此操作:
var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;
function sortAlphaNum(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;
}
}else if(isNaN(AInt)){//A is not an Int
return 1;//to make alphanumeric sort first return -1 here
}else if(isNaN(BInt)){//B is not an Int
return -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);
一个简单的方法是使用https://www.w3schools.com/jsref/jsref_localecompare.asp的 localeCompare() 方法JavaScript
例子:
export const sortAlphaNumeric = (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);
};
预期行为:
1000X Radonius Maximus
10X Radonius
200X Radonius
20X Radonius
20X Radonius Prime
30X Radonius
40X Radonius
Allegia 50 Clasteron
Allegia 500 Clasteron
Allegia 50B Clasteron
Allegia 51 Clasteron
Allegia 6R Clasteron
Alpha 100
Alpha 2
Alpha 200
Alpha 2A
Alpha 2A-8000
Alpha 2A-900
Callisto Morphamax
Callisto Morphamax 500
Callisto Morphamax 5000
Callisto Morphamax 600
Callisto Morphamax 6000 SE
Callisto Morphamax 6000 SE2
Callisto Morphamax 700
Callisto Morphamax 7000
Xiph Xlater 10000
Xiph Xlater 2000
Xiph Xlater 300
Xiph Xlater 40
Xiph Xlater 5
Xiph Xlater 50
Xiph Xlater 500
Xiph Xlater 5000
Xiph Xlater 58
您可以使用 Intl.Collator
与Read here 相比,它具有性能优势localeCompare
浏览器可比性(所有浏览器都支持)
let arr = ["A1", "A10", "A11", "A12", "A2", "A3", "A4", "B10", "B2", "F1", "F12", "F3"]
let op = arr.sort(new Intl.Collator('en',{numeric:true, sensitivity:'accent'}).compare)
console.log(op)