我有以下类
的数组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;
}
}
我希望按成功的价值对其进行排序(顶部为假,底部为真)。我怎么能这样做?
我试过
this.tests.sort((a,b)=> b.succress - a.succress);
但是没有做任何事情
答案 0 :(得分:1)
您可以按布尔值排序如下:
this.tests.sort((a, b) => {
if (a.succress) {
return -1;
}
if (b.succress) {
return 1;
}
return 0;
});
答案 1 :(得分:0)
答案 2 :(得分:0)
也许是这样的?
{{1}}