我的问题
如何通过Javascript中的另一个字符串数组对字符串数组进行排序?
背景
我正在寻找通过另一个数组排序字符串数组的方法。具体来说,我有一个问题,我不能按字母或数字排序。我知道Javascript支持自定义排序函数,如本例所示:
let numberArray = [40, 1, 5, 200];
function compareNumbers(a, b) {
return a - b;
}
numberArray.sort(compareNumbers);我不确定自定义字符串比较函数将如何工作,以解决我正在研究的问题。我在下面第1部分和第2部分中给出了两个示例数组。如果您需要进一步的澄清,请询问。
第1部分
示例数组:注意到educationSorted是按educationLevel排序education的理想结果。
let education = [
"High School",
"Middle School",
"Undergrad",
"Middle School",
"Middle School",
"Middle School",
"Middle School",
"Graduate",
"Elementary",
"Elementary",
"Elementary",
"Elementary"
];
let educationLevel = ["Elementary", "Middle School", "High School", "Undergrad", "Graduate"];
let educationSorted = [
"Elementary",
"Elementary",
"Elementary",
"Elementary",
"Middle School",
"Middle School",
"Middle School",
"Middle School",
"Middle School",
"High School",
"Undergrad",
"Graduate"
];第2部分
相同的一般问题,但字符串数组被数组替换。
let education = [
["Bob", "High School"],
["Alice", "Middle School"],
["Jane Doe", "Undergrad"],
["alex", "Middle School"],
["Mega Alice", "Middle School"],
["Jhon Doe", "Middle School"],
["Minor Bob", "Middle School"],
["Chicken", "Graduate"],
["Minor Chicken", "Elementary"],
["Clones", "Elementary"],
["Clones", "Elementary"],
["Clones", "Elementary"]
];
let educationLevel = ["Elementary", "Middle School", "High School", "Undergrad", "Graduate"];
let education = [
["Clones", "Elementary"],
["Clones", "Elementary"],
["Clones", "Elementary"],
["Minor Chicken", "Elementary"],
["Alice", "Middle School"],
["Mega Alice", "Middle School"],
["Jhon Doe", "Middle School"],
["Minor Bob", "Middle School"],
["alex", "Middle School"],
["Bob", "High School"],
["Jane Doe", "Undergrad"],
["Chicken", "Graduate"]
];发布于 2021-05-17 17:41:38
用这个:
let education = [
["Bob", "High School"],
["Alice", "Middle School"],
["Jane Doe", "Undergrad"],
["alex", "Middle School"],
["Mega Alice", "Middle School"],
["Jhon Doe", "Middle School"],
["Minor Bob", "Middle School"],
["Chicken", "Graduate"],
["Minor Chicken", "Elementary"],
["Clones", "Elementary"],
["Clones", "Elementary"],
["Clones", "Elementary"]
];
let educationLevel = ["Elementary", "Middle School", "High School", "Undergrad", "Graduate"];
function sortFunc(a, b) {
return educationLevel.indexOf(a[1]) - educationLevel.indexOf(b[1]);
}
education.sort(sortFunc);
console.log(education);
发布于 2021-05-17 17:31:56
您可以尝试使用这样的自定义排序函数:
const educationLevel = [
"Elementary",
"Middle School",
"High School",
"Undergrad",
"Graduate",
];
const education = [
["Bob", "High School"],
["Alice", "Middle School"],
["Jane Doe", "Undergrad"],
["alex", "Middle School"],
["Mega Alice", "Middle School"],
["Jhon Doe", "Middle School"],
["Minor Bob", "Middle School"],
["Chicken", "Graduate"],
["Minor Chicken", "Elementary"],
["Clones", "Elementary"],
["Clones", "Elementary"],
["Clones", "Elementary"],
];
const compareEntries = (a, b) => {
const aPriority = educationLevel.findIndex((element) => {
return element === a[1];
});
const bPriority = educationLevel.findIndex((element) => {
return element === b[1];
});
return aPriority - bPriority;
};
education.sort(compareEntries);此方法使用educationLevel数组的索引来确定排序顺序。
https://stackoverflow.com/questions/67574422
复制相似问题