I have the following TypeScript code:
export const cgGroups = [
{
id: 2,
name: 'North America & Caribbean'
},
{
id: 3,
name: 'Latin America'
},
{
id: 6,
name: 'Europe'
},
{
id: 4,
name: 'Asia Pacific'
},
{
id: 1,
name: 'Middle East & Africa'
},
{
id: 7,
name: 'International'
}
];
I want to sort the above alphabetical except one object
{
id: 7,
name: 'International'
}
which I want to move it to the last of the sorted array.
I tried the below code to sort:
cgGroups = cgGroups.map(({id, name}) => ({id, name})).sort((a, b) => {
if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
}
if (a.name.toLowerCase() < b.name.toLowerCase()) {
return -1;
}
return 0;
});
Here is the expected output:
Asia Pacific, Europe, Latin America, Middle East & Africa, North America & Caribbean, and International
Can anyone guide me here to fix this issue?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…