我們如何按姓氏按字母順序排列來自物件陣列的資料,這里要注意的是沒有像 FirstName 和 LastName 這樣的單獨屬性,該屬性將其保存為全名(請查看我的示例資料以下)
因此,如果我們按姓氏對資料進行字母排序,則結果將根據以下資料按此順序排列。
如果名字是“James van der Wal”、“Mary Tyler Moore”、“Erik the Great”和“Madonna”,還有一個問題呢?
處理這種情況的有效方法是什么?幫助將不勝感激,謝謝。目前我在下面有一個解決方案,但愿意接受輸入。謝謝。
Alexa Bermodes
Bryan Christian
Alen Geizer
Philipp Hym
Mattew Merrillos
Emil Ortizano
Ivana Turnerre
Steven Weinraucherche
#Object - 名稱
[
{
"id": 2,
"display": "Alen Geizer",
"subDisplay": null,
"attribute1": null,
"attribute2": null
},
{
"id": 9,
"display": "Emil Ortizano",
"subDisplay": null,
"attribute1": null,
"attribute2": null
},
{
"id": 10,
"display": "Philipp Hym",
"subDisplay": null,
"attribute1": null,
"attribute2": null
},
{
"id": 11,
"display": "Bryan Christian",
"subDisplay": null,
"attribute1": null,
"attribute2": null
},
{
"id": 12,
"display": "Ivana Turnerre",
"subDisplay": null,
"attribute1": null,
"attribute2": null
},
{
"id": 13,
"display": "Mattew Merrillos",
"subDisplay": null,
"attribute1": null,
"attribute2": null
},
{
"id": 1,
"display": "Alexa Bermudes",
"subDisplay": null,
"attribute1": null,
"attribute2": null
},
{
"id": 2299,
"display": "Steven Weinraucherche",
"subDisplay": null,
"attribute1": null,
"attribute2": null
}
]
#代碼
#代碼
getSampleListOfNames() {
this.isLoading = true;
this._sample.getSampleListNames(id, '')
.pipe(
finalize(() => this.isLoading = false),
).subscribe({
next: (res) => {
if (res.data) {
res.data.sort((a,b) => a.display.split(" ")[1] > b.display.split(" ")[1] ? 1: -1);
this.names = res.data;
}
},
error: err => noop,
complete: () => {
this.isLoading = false;
}
});
}
uj5u.com熱心網友回復:
如果沒有額外的外部輸入來描述名稱的構成,我使用的經驗法則是將名字定義為空格分隔的第一個單詞,并將姓氏定義為其余的......
接受這個想法,并用它進行排序......
const parseName = fullname => {
const tokens = fullname.split(' ');
const firstname = tokens[0];
const lastname = tokens.slice(1).join(' ');
return {
firstname,
lastname
};
};
const compareNames = (a, b) => {
const parseA = parseName(a);
const parseB = parseName(b);
return parseA.lastname.localeCompare(parseB.lastname);
};
let sorted = ["Mary Tyler Moore", "Madonna", "Erik The Great"].sort(compareNames);
console.log(sorted)
另一個編輯......對于物件,快取這個計算的決議名稱是值得的,以便應用程式的其他位可以使用它,例如在后續排序中......
const parseName = fullname => {
const tokens = fullname.split(' ');
const firstname = tokens[0];
const lastname = tokens.slice(1).join(' ');
return {
firstname,
lastname
};
};
// this new compare compares on the cached 'parsedName' prop
const compareObjects = (a, b) => {
return a.parsedName.lastname.localeCompare(b.parsedName.lastname);
};
// give each object a new parsedName prop
const augmentedData = getData().map(o => ({ parsedName: parseName(o.display), ...o }));
let sorted = augmentedData.sort(compareObjects);
console.log(sorted);
function getData() {
return [{
"id": 2,
"display": "Alen Geizer",
"subDisplay": null,
"attribute1": null,
"attribute2": null
},
{
"id": 9,
"display": "Emil Ortizano",
"subDisplay": null,
"attribute1": null,
"attribute2": null
}, {
"id": 10,
"display": "Philipp Hym",
"subDisplay": null,
"attribute1": null,
"attribute2": null
}, {
"id": 11,
"display": "Bryan Christian",
"subDisplay": null,
"attribute1": null,
"attribute2": null
}, {
"id": 12,
"display": "Ivana Turnerre",
"subDisplay": null,
"attribute1": null,
"attribute2": null
}, {
"id": 13,
"display": "Mattew Merrillos",
"subDisplay": null,
"attribute1": null,
"attribute2": null
},
{
"id": 1,
"display": "Alexa Bermudes",
"subDisplay": null,
"attribute1": null,
"attribute2": null
}, {
"id": 2299,
"display": "Steven Weinraucherche",
"subDisplay": null,
"attribute1": null,
"attribute2": null
}
]
}
由于另一個原因,快取決議是一個好主意:由于名字/姓氏的選擇有點隨意,決議的屬性使該決定明確且集中。有了它,使用這些物件的同事(或未來的你)可能會在將他們自己的文化直覺應用于名稱之前三思而后行,從而導致不一致。
uj5u.com熱心網友回復:
假設a = [as in question]and last name 表示 final 之后的字串" ",您可以這樣做:
const last = o => o.display.split(" ").pop();
a.sort((a, b) => last(a).localeCompare(last(b)))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/417399.html
標籤:
