Mr.*_*per 1 arrays sorting rxjs angular angular7
我需要帮助,我需要按 PendingQuantity 字段对数组进行排序。我有负数和正数。所以我的代码:
this.data.Products.sort(obj => obj.PendingQuantity);
Run Code Online (Sandbox Code Playgroud)
所以我的数组
"Products": [
{
"ProductCode": "MC30180",
"Description": "Description_1",
"NationalCode": "N.C. 0965",
"PendingQuantity": 20,
"toBeScanned": true
},
{
"ProductCode": "Name_2",
"Description": "Description_2",
"NationalCode": "N.C. 0382",
"PendingQuantity": -3,
"toBeScanned": false
},
{
"ProductCode": "Name_3",
"Description": "Description_3",
"NationalCode": "N.C. 8913",
"PendingQuantity": 25,
"toBeScanned": false
}
]
Run Code Online (Sandbox Code Playgroud)
我想要的顺序是:
"Products": [
{
"ProductCode": "MC30180",
"Description": "Description_1",
"NationalCode": "N.C. 0965",
"PendingQuantity": -3,
"toBeScanned": true
},
{
"ProductCode": "Name_2",
"Description": "Description_2",
"NationalCode": "N.C. 0382",
"PendingQuantity": 25,
"toBeScanned": false
},
{
"ProductCode": "Name_3",
"Description": "Description_3",
"NationalCode": "N.C. 8913",
"PendingQuantity": 20,
"toBeScanned": false
}
]
Run Code Online (Sandbox Code Playgroud)
您可以将其用于订单 asc product = product.sort((a, b) => a.PendingQuantity - b.PendingQuantity);
如果您想按 desc 订购,请使用 product = product.sort((a, b) => b.PendingQuantity - a.PendingQuantity);
更新:
如果您显示自定义顺序,如 -3、25、20,则可以处理排序条件。
product = product.sort((a, b) => { if (a.PendingQuantity < 0) {return -1; } if (b.PendingQuantity <0 ) {return 1;} return b.PendingQuantity - a.PendingQuantity });
Run Code Online (Sandbox Code Playgroud)
product = product.sort((a, b) => { if (a.PendingQuantity < 0) {return -1; } if (b.PendingQuantity <0 ) {return 1;} return b.PendingQuantity - a.PendingQuantity });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1811 次 |
| 最近记录: |