【发布时间】:2021-10-13 09:51:36
【问题描述】:
我有一个对象数组,我正在尝试按类型和代码对它们的信息进行排序。 我的目标是首先获取 type type = true 的行,然后按代码升序排序。
我试过这个,但他把假的放在最上面,并且没有按升序对代码进行排序。
谁能帮帮我?
.TS
data = [{
cod: 1.19,
type: true
},
{
cod: 1.13,
type: true
},
{
cod: 1.01,
type: true
},
{
cod: 1.1,
type: false
},
{
cod: 1.2,
type: true
},
{
cod: 2.19,
type: false
},
{
cod: 3.29,
type: true
},
{
cod: 5.19,
type: false
},
{
cod: 1.7,
type: false
},]
ngOnInit(){
this.data.sort(function (x, y) {
return (x.type === y.type) ? 0 : x ? -1 : 1;;
});
console.log(this.data)
}
【问题讨论】:
-
那么,您想先按类型排序,然后按代码排序,首先使用 type == true 吗?
return x.type ? -1 : x.cod - y.cod; -
@HereticMonkey 是的,没错
标签: javascript angular typescript sorting