0

私は次の配列を持っています:

[
    {
        "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/"
    }
]

「貯蓄」の多い順に並べ替えたい。次のコードを使用しています

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;
});

次の結果が得られます。

[
    {
        "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/"
    }
]

基本的に、保存が文字列である配列項目の上に並べ替えています。私はそれらを一番下にしたい。

4

4 に答える 4

3

0存在しないように見えるオブジェクトのプロパティにアクセスしようとしているようです。それを修正すると同時に、何が起こっているのかを簡単に確認できるようにします。

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;
});
于 2013-09-03T12:17:27.700 に答える
0

比較関数が に提供されると、配列自体ではなく、元の配列の要素であるArray.prototype.sort引数aおよびが与えられます。b

したがって、この特定のケースでは、ドロップする必要があります[0]。たとえば、次の代わりに:

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

試す:

if (a.Saving === b.Saving) { ...
           // ^-- also, it's best to use strict comparison
于 2013-09-03T12:17:15.730 に答える