【问题标题】:Sort array of objects by containing string通过包含字符串对对象数组进行排序
【发布时间】:2025-12-28 11:30:11
【问题描述】:

我正在尝试按子字符串/字符串对对象数组进行排序

var substring = 'nord';
var arrayOfObjs = [0: {label: "Nordals", value: "s29"}
1: {label: "Skåstrup Strand", value: "s12"}
2: {label: "Blokhus", value: "s61"}
3: {label: "Fanø", value: "s27"}
4: {label: "Rømø", value: "s26"}
5: {label: "Vorupør", value: "s66"}
6: {label: "Grønhøj", value: "s41"}
7: {label: "Nr. Lyngby ved Løkken", value: "s14"}
8: {label: "Nordjylland", value: "6"}
9: {label: "Nordsjælland", value: "7"}
10: {label: "Vestjylland", value: "10"}
11: {label: "Tyskland", value: "13"}];

我有这个功能要排序:

function compare(a, b) {
  // Use toUpperCase() to ignore character casing
  const areaA = a.label.toUpperCase();
  const areaB = b.label.toUpperCase();

  let comparison = 0;
  if (areaA > areaB) {
    comparison = 1;
  } else if (areaA < areaB) {
    comparison = -1;
  }
  return comparison;
}

排序前:

0: {label: "Nordals", value: "s29"}
1: {label: "Skåstrup Strand", value: "s12"}
2: {label: "Blokhus", value: "s61"}
3: {label: "Fanø", value: "s27"}
4: {label: "Rømø", value: "s26"}
5: {label: "Vorupør", value: "s66"}
6: {label: "Grønhøj", value: "s41"}
7: {label: "Nr. Lyngby ved Løkken", value: "s14"}
8: {label: "Nordjylland", value: "6"}
9: {label: "Nordsjælland", value: "7"}
10: {label: "Vestjylland", value: "10"}
11: {label: "Tyskland", value: "13"}

排序后:

0: {label: "Blokhus", value: "s61"}
1: {label: "Fanø", value: "s27"}
2: {label: "Grønhøj", value: "s41"}
3: {label: "Nordals", value: "s29"}
4: {label: "Nordjylland", value: "6"}
5: {label: "Nordsjælland", value: "7"}
6: {label: "Nr. Lyngby ved Løkken", value: "s14"}
7: {label: "Rømø", value: "s26"}
8: {label: "Skåstrup Strand", value: "s12"}
9: {label: "Tyskland", value: "13"}
10: {label: "Vestjylland", value: "10"}
11: {label: "Vorupør", value: "s66"}

对象数组按字母排序,但我需要按子字符串对arrayOfObjects 进行排序,所以基本上每个最接近substring 变量的arrayOfObjs[x].label 都必须在排序结果中排在第一位?

如何添加此功能?

它应该看起来像这样,因为 3 首先包含 'nord' 字符串:

0: {label: "Nordals", value: "s29"}
1: {label: "Nordjylland", value: "6"}
2: {label: "Nordsjælland", value: "7"}
3: {label: "Blokhus", value: "s61"}
4: {label: "Fanø", value: "s27"}
5: {label: "Grønhøj", value: "s41"}
6: {label: "Nr. Lyngby ved Løkken", value: "s14"}
7: {label: "Rømø", value: "s26"}
8: {label: "Skåstrup Strand", value: "s12"}
9: {label: "Tyskland", value: "13"}
10: {label: "Vestjylland", value: "10"}
11: {label: "Vorupør", value: "s66"}

我尝试浏览堆栈溢出,但找不到任何东西,如果这是重复的,请告诉我路径。

【问题讨论】:

  • 你可以使用任何解决方案,我更新了你的代码,希望对你有帮助,谢谢

标签: javascript arrays sorting javascript-objects


【解决方案1】:

您可以先按子字符串排序,然后按字母排序。

var array = [{ label: "Nordals", value: "s29" }, { label: "Skåstrup Strand", value: "s12" }, { label: "Blokhus", value: "s61" }, { label: "Fanø", value: "s27" }, { label: "Rømø", value: "s26" }, { label: "Vorupør", value: "s66" }, { label: "Grønhøj", value: "s41" }, { label: "Nr. Lyngby ved Løkken", value: "s14" }, { label: "Nordjylland", value: "6" }, { label: "Nordsjælland", value: "7" }, { label: "Vestjylland", value: "10" }, { label: "Tyskland", value: "13" }],
    string = 'nord';

array.sort((a, b) =>
    b.label.toLowerCase().includes(string) - a.label.toLowerCase().includes(string) ||
    a.label.localeCompare(b.label)
);

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

经典功能的IE版和String#indexOf

array.sort(function (a, b) {
    return (a.label.toLowerCase().indexOf(string) === -1) - (b.label.toLowerCase().indexOf(string) === -1)
        || a.label.localeCompare(b.label);
});

【讨论】:

  • 谢谢尼娜,正是我要找的东西
  • 有没有办法让这个 IE 兼容,不支持包含
  • 你真是太棒了,非常感谢:)!
【解决方案2】:

首先你必须对字符串应用搜索然后排序,即

function compare(a, b) {
  // Use toUpperCase() to ignore character casing
  const areaA = a.label.toUpperCase();
  const areaB = b.label.toUpperCase();

  let comparison = 0;
  if(areaA .includes("substring")){ // check if sub-string available 
  if (areaA > areaB) {
    comparison = 1;
  } else if (areaA < areaB) {
    comparison = -1;
  }
}else{
  //based on where you put the unsorted object (1 or -1)
}
  return comparison;
}

【讨论】:

    【解决方案3】:

    这应该可行:

      arrayOfObjs.sort((a, b) => (a.label.toLowerCase().match(/nord/) != null) ? -1 : (b.label.toLowerCase().match(/nord/) != null) ? 1 : (a.label > b.label) ? 1: -1);
    

    现场演示:https://stackblitz.com/edit/js-9m946j

    【讨论】:

      最近更新 更多