In what way can we organize the data from an array of objects alphabetically by last name when there is no separate property for first and last names, but rather a single property called fullname (see example data below)?
If we were to sort the data by last name, it would result in the following order based on the provided data.
One challenge that arises is dealing with names such as "James van der Wal," "Mary Tyler Moore," "Erik the Great," and "Madonna." How can this scenario be efficiently managed? Any assistance would be greatly appreciated. Thank you. I currently have a solution outlined below but am open to suggestions. Thank you.
Alexa Bermodes
Bryan Christian
Alen Geizer
Philipp Hym
Mattew Merrillos
Emil Ortizano
Ivana Turnerre
Steven Weinraucherche
#Object - names
[
{
"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
}
]
#code
#code
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;
}
});
}