首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将字符串数组按另一个字符串数组排序的最有效方法是什么?

将字符串数组按另一个字符串数组排序的最有效方法是什么?
EN

Stack Overflow用户
提问于 2021-05-17 17:28:16
回答 2查看 135关注 0票数 0

我的问题

如何通过Javascript中的另一个字符串数组对字符串数组进行排序?

背景

我正在寻找通过另一个数组排序字符串数组的方法。具体来说,我有一个问题,我不能按字母或数字排序。我知道Javascript支持自定义排序函数,如本例所示:

代码语言:javascript
复制
let numberArray = [40, 1, 5, 200];
function compareNumbers(a, b) {
  return a - b;
}
numberArray.sort(compareNumbers);

我不确定自定义字符串比较函数将如何工作,以解决我正在研究的问题。我在下面第1部分和第2部分中给出了两个示例数组。如果您需要进一步的澄清,请询问。

第1部分

示例数组:注意到educationSorted是按educationLevel排序education的理想结果。

代码语言:javascript
复制
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部分

相同的一般问题,但字符串数组被数组替换。

代码语言:javascript
复制
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"]
];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-17 17:41:38

用这个:

代码语言:javascript
复制
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);

票数 1
EN

Stack Overflow用户

发布于 2021-05-17 17:31:56

您可以尝试使用这样的自定义排序函数:

代码语言:javascript
复制
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数组的索引来确定排序顺序。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67574422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档