[英]Sort array of objects by containing 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"}];
我有这个 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;
}
排序前:
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"}
对象数组按字母表排序,但我需要按 substring 对arrayOfObjects
进行排序,所以基本上每个arrayOfObjs[x].label
最接近substring
变量的结果排在第一位?
我将如何添加此功能?
它应该看起来像这样,因为 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"}
我尝试浏览堆栈溢出,但找不到任何东西,如果这是重复的,请告诉我路径。
您可以按 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; }
带有经典 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);
});
首先你必须对字符串应用搜索然后排序即
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;
}
这应该有效:
arrayOfObjs.sort((a, b) => (a.label.toLowerCase().match(/nord/) != null) ? -1 : (b.label.toLowerCase().match(/nord/) != null) ? 1 : (a.label > b.label) ? 1: -1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.