2017-12-05 21 views
-1

아래 코드를 사용하여 jsonarray를 정렬하려고했습니다. 하지만 jsonarray를 정확하게 분류하지는 않습니다. 일부 값이 잘못 배치되었습니다.가격을 기준으로 jsonarray를 정렬합니다.

내 Jsonarray는

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 문자열이나 오름차순 수치와 내림차순을 기반으로하는 알고 있나요

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

코드를입니다. 어떤 긍정적 인 반응이라도 미리 감사드립니다.

+0

변경 가격이 ... [분명히 당신이 얻고있는'ClassCastException'] (https://ideone.com/0mmSDE) ...도 ... "좋은" 예외 처리'catch (JSONException e) { // 무언가를 }'' – Selvin

답변

0

귀하의 비교 클래스는 이와 같아야합니다. 당신의 JSON의 숫자에

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