根据价格对jsonarray进行排序

时间:2017-12-05 11:34:50

标签: android sorting comparator

我尝试使用下面的代码对jsonarray进行排序。但它没有正确排序jsonarray。一些价值观错位。

我的Jsonarray

{
  "sTATUS": "SUCCESS",
  "mSG": "Product List",
  "product_list": [
    {
      "product_id": "5",
      "product_name": "LAS VEGAS  530",
      "product_model": "530",
      "product_image": "uploads/main/14748883885prime.png",
      "price": "18990",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "0",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "0"
    },
    {
      "product_id": "68",
      "product_name": "TESTAPESKER  MODSPK1",
      "product_model": "modspk1",
      "product_image": "",
      "price": "2100",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "0",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "0"
    },
    {
      "product_id": "1",
      "product_name": "CAPE TOWN  940",
      "product_model": "940",
      "product_image": "uploads/main/14748836241prime.png",
      "price": "49990",
      "discount_price": "990",
      "discount_percentage": "1.98%",
      "offer_price": "49000",
      "featured_product": "1",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "3",
      "review": "2",
      "gift_id": "0"
    },
    {
      "product_id": "10",
      "product_name": "BARCELONA  230 BT",
      "product_model": "230 BT",
      "product_image": "uploads/main/147531739810prime.png",
      "price": "10990",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "1",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "0"
    },
    {
      "product_id": "3",
      "product_name": "SANTA FE  530",
      "product_model": "530",
      "product_image": "uploads/main/14748874843prime.png",
      "price": "34990",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "1",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "0"
    },
    {
      "product_id": "8",
      "product_name": "NEW ORLEANS  220",
      "product_model": "220",
      "product_image": "uploads/main/14753168598prime.png",
      "price": "8990",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "0",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "0"
    },
    {
      "product_id": "6",
      "product_name": "SAN MARINO  330",
      "product_model": "330",
      "product_image": "uploads/main/14748893666prime.png",
      "price": "14990",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "0",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "0"
    },
    {
      "product_id": "7",
      "product_name": "NEW JERSEY  220BT",
      "product_model": "220BT",
      "product_image": "uploads/main/14753166327prime.png",
      "price": "10990",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "0",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "0"
    },
    {
      "product_id": "13",
      "product_name": "TOKYO  110",
      "product_model": "110",
      "product_image": "uploads/main/147549157513prime.png",
      "price": "4990",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "0",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "2"
    },
    {
      "product_id": "2",
      "product_name": "SAN ANTONIO  640",
      "product_model": "640",
      "product_image": "uploads/main/14748866592prime.png",
      "price": "33500",
      "discount_price": "500",
      "discount_percentage": "1.49%",
      "offer_price": "33000",
      "featured_product": "0",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "1"
    },
    {
      "product_id": "60",
      "product_name": "PHILADELPHIA  845",
      "product_model": "845",
      "product_image": "uploads/main/148109520160prime.png",
      "price": "54990",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "1",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "0"
    },
    {
      "product_id": "12",
      "product_name": "MANCHESTER  110",
      "product_model": "110",
      "product_image": "uploads/main/147549119212prime.png",
      "price": "5990",
      "discount_price": "100",
      "discount_percentage": "1.67%",
      "offer_price": "5890",
      "featured_product": "0",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "0"
    },
    {
      "product_id": "11",
      "product_name": "MONTEVIDEO  4010 DVD",
      "product_model": "4010 DVD",
      "product_image": "uploads/main/147549071111prime.png",
      "price": "9990",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "0",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "0"
    },
    {
      "product_id": "4",
      "product_name": "SAN DIEGO  530",
      "product_model": "530",
      "product_image": "uploads/main/14748877994prime.png",
      "price": "31990",
      "discount_price": "",
      "discount_percentage": "",
      "offer_price": "",
      "featured_product": "1",
      "category_id": "1",
      "category_name": "Speakers",
      "rate": "0",
      "review": "0",
      "gift_id": "2"
    }
  ],
  "cart_count": "0"
}

    try {
        JSONObject json=new JSONObject(cal_json);
        JSONArray users = json.getJSONArray("product_list");
        List<JSONObject> list = new ArrayList<JSONObject>();
        for (int i = 0; i < users.length(); i++) {
            list.add(users.getJSONObject(i));
        }
        Collections.sort( list, new Comparator<JSONObject>() {

            public int compare(JSONObject a, JSONObject b) {
                Long valA = null;
                Long valB=null;

                try {
                    valA = (Long) a.get("price");
                    valB = (Long) b.get("price");
                }
                catch (JSONException e) {
                    //do something
                }

                return valA.compareTo(valB);
            }
        });
        // Collections.sort(list, new SortBasedOnMessageId());
        JSONArray resultArray = new JSONArray();
        for (int i = 0; i < users.length(); i++) {
            resultArray.put(list.get(i));
        }
        Log.d("newjsonarr",resultArray.toString());
        getjson(resultArray.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

有没有人知道基于字符串或数值按升序和降序排序jsonarray的确切方法。对于任何积极的回应,请提前感谢。

1 个答案:

答案 0 :(得分:0)

你的比较课应该是这样的。

        public int compare(JSONObject a, JSONObject b) {
            Long valA = null;
            Long valB=null;

            try {
                valA = Long.parseLong(a.get("price"));
                valB = Long.parseLong(b.get("price"));
            }
            catch (JSONException e) {
                //do something
            }

            return valA.compareTo(valB);
        }
    });