简体   繁体   English

通过包含字符串对对象数组进行排序

[英]Sort array of objects by containing string

I am trying to sort an array of objects by substring/string我正在尝试按子字符串/字符串对对象数组进行排序

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

I have this function to sort:我有这个 function 要排序:

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;
}

Before sorting:排序前:

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"}

After sorting:排序后:

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"}

The array of objects is being sorted by the alphabet, but I need to sort the arrayOfObjects by the substring, so basically every arrayOfObjs[x].label that is closest to the substring variable has to come first in the sorted results?对象数组按字母表排序,但我需要按 substring 对arrayOfObjects进行排序,所以基本上每个arrayOfObjs[x].label最接近substring变量的结果排在第一位?

How would I add this functionality?我将如何添加此功能?

It should look something like this because the 3 first contains the 'nord' string:它应该看起来像这样,因为 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"}

I have tried browsing through stack overflow, but I was unable to find anything, if this is a duplicate please show me the path.我尝试浏览堆栈溢出,但找不到任何东西,如果这是重复的,请告诉我路径。

You could sort by substring and then by alphabet.您可以按 substring 排序,然后按字母排序。

 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 version with classic function and String#indexOf .带有经典 function 和String#indexOf的 IE 版本。

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

First you have to apply search on String then sorting ie首先你必须对字符串应用搜索然后排序即

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;
}

This should work:这应该有效:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM