【问题标题】:What is the most efficient way to sort an array of strings by another array of strings? [duplicate]用另一个字符串数组对一个字符串数组进行排序的最有效方法是什么? [复制]
【发布时间】:2021-08-06 23:52:40
【问题描述】:

我的问题

如何在 Javascript 中按另一个字符串数组对字符串数组进行排序?

背景

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

let numberArray = [40, 1, 5, 200];
function compareNumbers(a, b) {
  return a - b;
}
numberArray.sort(compareNumbers);

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

第 1 部分

示例数组:请注意,educationSorted 是通过educationLeveleducation 进行排序的期望结果。

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"]
];

【问题讨论】:

  • 我知道问题已结束,但我更新了我的答案,专门针对您要解决的问题。

标签: javascript arrays string sorting


【解决方案1】:

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

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 数组的索引来确定排序顺序。

【讨论】:

    【解决方案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"];
    
    function sortFunc(a, b) {
      return educationLevel.indexOf(a[1]) - educationLevel.indexOf(b[1]);
    }
    
    education.sort(sortFunc);
    console.log(education);

    【讨论】:

    • 该死的应该想到这一点。谢谢。
    猜你喜欢
    • 2019-08-07
    • 1970-01-01
    • 2012-03-23
    • 2021-03-08
    • 2014-09-03
    • 2021-06-25
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多