Javascript 按包含字符串对对象数组排序

Javascript 按包含字符串对对象数组排序,javascript,arrays,sorting,javascript-objects,Javascript,Arrays,Sorting,Javascript Objects,我正在尝试按子字符串/字符串对对象数组进行排序 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

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

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"}
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"}
排序后:

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"}
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"}
对象数组是按字母表排序的,但我需要按子字符串对
arrayOfObjects
进行排序,因此基本上每个
arrayOfObjs[x]都是如此。在排序结果中,最接近
子字符串的标签必须排在第一位

如何添加此功能

它应该是这样的,因为3首先包含'nord'字符串:

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

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

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

var数组=[{label:“Nordals”,value:“s29”},{label:“Skåstrup Strand”,value:“s12”},{label:“Blokhus”,value:“s61”},{label:“Fanø”,value:“s27”},{label:“RømøR”,value:“s66”},{label:“Gr nh j”,value:“s41”},{label:“Nr.Lyngby ved Løkken”,value:“s14”{label:“Nordjylland”,价值:“6”},{标签:“Nordsjælland”,价值:“7”},{标签:“Vestjylland”,价值:“10”},{标签:“Tyskland”,价值:“13”},
字符串='nord';
array.sort((a,b)=>
b、 label.toLowerCase().includes(字符串)-a.label.toLowerCase().includes(字符串)||
a、 label.localeCompare(b.label)
);
console.log(数组);

。作为控制台包装{max height:100%!important;top:0;}
您可以按子字符串排序,然后按字母表排序

var数组=[{label:“Nordals”,value:“s29”},{label:“Skåstrup Strand”,value:“s12”},{label:“Blokhus”,value:“s61”},{label:“Fanø”,value:“s27”},{label:“RømøR”,value:“s66”},{label:“Gr nh j”,value:“s41”},{label:“Nr.Lyngby ved LøkkenNordjylland”,值:“6”},{标签:“Nordsjælland”,值:“7”},{标签:“Vestjylland”,值:“10”},{标签:“Tyskland”,值:“13”},
字符串='nord';
array.sort((a,b)=>
b、 label.toLowerCase().includes(字符串)-a.label.toLowerCase().includes(字符串)||
a、 label.localeCompare(b.label)
);
console.log(数组);

。作为控制台包装{max height:100%!important;top:0;}
首先必须对字符串应用搜索,然后进行排序,即

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;
}
功能比较(a、b){
//使用toUpperCase()忽略字符大小写
const areaA=a.label.toUpperCase();
const areaB=b.label.toUpperCase();
让比较=0;
if(areaA.includes(“子字符串”){//检查子字符串是否可用
如果(区域A>区域B){
比较=1;
}否则,如果(区域A<区域B){
比较=-1;
}
}否则{
//基于放置未排序对象的位置(1或-1)
}
收益比较;
}

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

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;
}
功能比较(a、b){
//使用toUpperCase()忽略字符大小写
const areaA=a.label.toUpperCase();
const areaB=b.label.toUpperCase();
让比较=0;
if(areaA.includes(“子字符串”){//检查子字符串是否可用
如果(区域A>区域B){
比较=1;
}否则,如果(区域A<区域B){
比较=-1;
}
}否则{
//基于放置未排序对象的位置(1或-1)
}
收益比较;
}
这应该可以:

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

这应该可以:

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

现场演示:

谢谢你,尼娜,我一直在寻找的是,有没有办法让这个IE兼容,不适用于Include你太棒了,非常感谢:)!谢谢你,尼娜,我一直在寻找的是,有没有办法让这个IE兼容,不适用于Include你太棒了,谢谢你这么多:)!你可以使用任何解决方案,我更新了你的代码,希望它有帮助,谢谢你可以使用任何解决方案,我更新了你的代码,希望它有帮助,谢谢