Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array Angular 7 by number field on decreasing or increase order

i need an help, i need to sort array by PendingQuantity field. I've negative and positive numbers. So my code:

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

So my array

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

the order i want is:

"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
                }
            ]
like image 467
Mr. Developer Avatar asked Oct 23 '25 17:10

Mr. Developer


1 Answers

You can use this for order asc product = product.sort((a, b) => a.PendingQuantity - b.PendingQuantity);

If you want to order by desc use product = product.sort((a, b) => b.PendingQuantity - a.PendingQuantity);

Update:

If you show custom order like -3, 25, 20, you can handle sorting condition.

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);
like image 143
Hien Nguyen Avatar answered Oct 26 '25 08:10

Hien Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!