user2129794
user2129794

Reputation: 2418

Not able to sort array of objects

I have following array:

[
    {
        "BestCoupon": 1,
        "Saving": "100",
        "Successful": 1,
        "couponCode": "CRIC100",
        "description": "Get Rs. 100 Discount on purchase of Rs. 599 & above. Products include Eyeglasses & Sunglasses and more. Choose",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE :Coupon code \"CRIC200\" is not valid.",
        "Successful": 0,
        "couponCode": "CRIC200",
        "description": "Get Rs. 200 Discount on purchase of Rs. 999 & above. Products include Eyeglasses & Sunglasses and more. Choose",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"CRIC300\" is not valid.",
        "Successful": 0,
        "couponCode": "CRIC300",
        "description": "Get Rs. 300 Discount on purchase of Rs. 1199 & above. Products include Eyeglasses, Sunglasses & Contact Lenses",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE: Coupon code \"JJPREMIUM\" is not valid.",
        "Successful": 0,
        "couponCode": "JJPREMIUM",
        "description": "Get Rs 600 Discount on John Jacobs Eyeglasses. Products include Eyeglasses. Choose from brand like John",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE: Coupon code \"LENS20130303\" is not valid.",
        "Successful": 0,
        "couponCode": "LENS20130303",
        "description": "Get Rs. 500 Discount on purchase of Rs. 1000. Products include Eyeglasses, Sunglasses & Contact Lenses and",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "179.8",
        "Successful": 1,
        "couponCode": "LKEND20",
        "description": "Get 20% Discount on Eyeglasses. Minimum Purchase Rs. 499. Products include Eyeglasses. Choose from brandslike",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "224.75",
        "Successful": 1,
        "couponCode": "LKEND25",
        "description": "Flat 25% off on purchase of Rs 799 & above",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"LKEND30\" is not valid.",
        "Successful": 0,
        "couponCode": "LKEND30",
        "description": "Get 30% Discount on purchase of Rs 1299 & above. Products include Eyeglasses, Sunglasses & Contact Lenses and",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"LKEND35\" is not valid.",
        "Successful": 0,
        "couponCode": "LKEND35",
        "description": "Get 35% Discount on purchase of Rs 1499 & above. Products include Eyeglasses, Sunglasses & Contact Lenses and",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "100",
        "Successful": 1,
        "couponCode": "OMGABV600",
        "description": "Get Rs. 100 Discounton order of Rs. 600 & Above. Products include Eyeglasses, Sunglasses & Contact Lenses and",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"POWERSUN20\" isnot valid.",
        "Successful": 0,
        "couponCode": "POWERSUN20",
        "description": "Get 20% OFFon Power Sunglasses. Select from a vast collection.",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "179.8",
        "Successful": 1,
        "couponCode": "WEEKEND20",
        "description": "Flat 20% OFF on purchase of Rs 499 & above - NA on Premium Brands & Contact Lenses",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    }
]

I want to sort it in the descending order of "Savings". Am using the following code

couponObj.sort(function (a, b) {
      if (a[0].Saving == b[0].Saving)
         return 0;
      if (a[0].Saving > b[0].Saving)
         return -1;
      if (a[0].Saving < b[0].Saving)
         return 1;
});

Its giving me the following result :

[
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"POWERSUN20\" is not valid.",
        "Successful": 0,
        "couponCode": "POWERSUN20",
        "description": "Get 20% OFF on Power Sunglasses. Select from a vast collection.",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"LKEND35\" is not valid.",
        "Successful": 0,
        "couponCode": "LKEND35",
        "description": "Get 35% Discount on purchase of Rs 1499 & above. Products include Eyeglasses, Sunglasses & Contact Lenses and",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"LKEND30\" is not valid.",
        "Successful": 0,
        "couponCode": "LKEND30",
        "description": "Get 30% Discount on purchase of Rs 1299 & above. Products include Eyeglasses, Sunglasses & Contact Lenses and",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"LENS20130303\" is not valid.",
        "Successful": 0,
        "couponCode": "LENS20130303",
        "description": "Get Rs. 500 Discount on purchase of Rs. 1000. Products include Eyeglasses, Sunglasses & Contact Lenses and",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"JJPREMIUM\" is not valid.",
        "Successful": 0,
        "couponCode": "JJPREMIUM",
        "description": "Get Rs 600 Discount on John Jacobs Eyeglasses. Products include Eyeglasses. Choose from brand like John",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"CRIC300\" is not valid.",
        "Successful": 0,
        "couponCode": "CRIC300",
        "description": "Get Rs. 300 Discount on purchase of Rs. 1199 & above. Products include Eyeglasses, Sunglasses & Contact Lenses",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "NOT APPLICABLE : Coupon code \"CRIC200\" is not valid.",
        "Successful": 0,
        "couponCode": "CRIC200",
        "description": "Get Rs. 200 Discount on purchase of Rs. 999 &above. Products include Eyeglasses & Sunglasses and more. Choose",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "224.75",
        "Successful": 1,
        "couponCode": "LKEND25",
        "description": "Flat 25% off on purchase of Rs 799 & above",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "179.8",
        "Successful": 1,
        "couponCode": "LKEND20",
        "description": "Get 20% Discount on Eyeglasses. Minimum Purchase Rs. 499. Products include Eyeglasses. Choose from brands like",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "179.8",
        "Successful": 1,
        "couponCode": "WEEKEND20",
        "description": "Flat 20%OFF on purchase of Rs 499 & above - NA on Premium Brands & Contact Lenses",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "100",
        "Successful": 1,
        "couponCode": "CRIC100",
        "description": "Get Rs. 100 Discount on purchase of Rs. 599 & above. Products include Eyeglasses & Sunglasses and more. Choose",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    },
    {
        "BestCoupon": 0,
        "Saving": "100",
        "Successful": 1,
        "couponCode": "OMGABV600",
        "description": "Get Rs. 100 Discount on order of Rs. 600 & Above. Products include Eyeglasses, Sunglasses & Contact Lenses and",
        "domain": "www.lenskart.com",
        "url": "http://www.lenskart.com/checkout/cart/"
    }
]

Basically it's sorting on top the array item where saving is a string. I want them at bottom.

Upvotes: 0

Views: 154

Answers (4)

Paul S.
Paul S.

Reputation: 66334

Looks like you're trying to access a property 0 of your object which doesn't appear to exist; and at the same time as correcting that, make it easier to see what's going on.

couponObj.sort(function (a, b) {
    a = +a.Saving, b = +b.Saving; // cast Number
    // special cases
    if (a !== a || b !== b)           // If there is a NaN
        return (a !== a) - (b !== b); // move it to the end
    // classic descending sort
    return b - a;
});

Upvotes: 3

Krešimir Prcela
Krešimir Prcela

Reputation: 4281

Use the parseFloat method for the string conversion to floats:

couponObj.sort(function (a, b) {
  if(a && b) {
    var af = parseFloat(a.Saving);
    var bf = parseFloat(b.Saving);
    if(isNan(af)) return 1;
    if(isNan(bf)) return -1;
    return bf-af;
  }
  return 0; // ignore null entries
}

Upvotes: 0

Oleg
Oleg

Reputation: 9359

When the comparator function is supplied to Array.prototype.sort, it is given arguments a and b which are elements of the original array, not the array itself.

So, in this particular case, you need to drop [0]. For example, instead of:

if (a[0].Saving == b[0].Saving) { ...

try:

if (a.Saving === b.Saving) { ...
           // ^-- also, it's best to use strict comparison

Upvotes: 0

xanatos
xanatos

Reputation: 111860

As other have suggested, but with a caveat: you have various Saving that are text instead of descriptions...

couponObj.sort(function (a, b) {
    a = parseFloat(a.Saving); // cast to number
    b = parseFloat(b.Saving); // cast to number

    if (isNaN(a)) return isNaN(b) ? 0 : 1;
    if (isNaN(b)) return -1;

    return b - a; // descending order
});

When you try casting a non-number to a number with +, the returned number is NaN (a special value that doesn't sort very well). For this reason the various if (isNaN())

Note that in initial versions I had used +a.Saving to cast the string to a number, but I've changed it to parseFloat. There is a subtle difference with null: +null === 0, while parseFloat(null) === NaN (that then I can handle).

Upvotes: 1

Related Questions