I have array of following class
export class Tests {
id: number;
name: string;
createdAt: any;
succress: boolean;
constructor(id: number, name: string, createdAt: any, success: boolean) {
this.id = id;
this.name = name;
this.createdAt = createdAt;
this.succress = success;
}
}
And I want to sort it by value of success (false on top and true on bottom). How can I do that?
I've tried
this.tests.sort((a,b)=> b.succress - a.succress);
But is not doing anything
You can sort by boolean value as follows:
this.tests.sort((a, b) => {
if (a.succress === b.succress) {
return 0;
}
if (a.succress) {
return -1;
}
if (b.succress) {
return 1;
}
});
Maybe something like this?
[false,true,false,true]
.sort(
(a,b)=>
(a===b)?0
:(a===true)?1:-1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With