【问题标题】:Sort array Angular 7 by number field on decreasing or increase order按递减或递增顺序按数字字段对数组 Angular 7 进行排序
【发布时间】:2019-03-29 11:00:21
【问题描述】:

我需要帮助, 我需要按 PendingQuantity 字段对数组进行排序。我有负数和正数。 所以我的代码:

this.data.Products.sort(obj => obj.PendingQuantity);

所以我的数组

"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
            }
        ]

我想要的顺序是:

"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
                }
            ]

【问题讨论】:

    标签: arrays angular sorting rxjs angular7


    【解决方案1】:

    您可以使用它来订购 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 });
    

    let product = [
                {
                    "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
                }
            ];
            
            
    product = product.sort((a, b) => a.PendingQuantity - b.PendingQuantity);
    product = product.sort((a, b) => { if (a.PendingQuantity < 0) {return -1; } if (b.PendingQuantity <0 ) {return 1;} return b.PendingQuantity - a.PendingQuantity });
    
    console.log(product);

    【讨论】:

    • 我需要这样排序:-3 first, 25 second, 20 第三个元素,你的例子中处理的负数?
    【解决方案2】:

    你可以使用Arraysort函数

    products.sort((a,b)=> a['PendingQuantity'] - b['PendingQuantity']);
    

    【讨论】:

    • 上面是升序,如果你想降序,那么试试:product.sort((a, b) =&gt; -(a.PendingQuantity - b.PendingQuantity));
    • 抱歉,您的脚本可以运行,但最后显示的是负数,有一种方法可以像命令一样使用 -3、25、20 吗?
    最近更新 更多