JAVA Sort JSONObject (String,Int) in descending order
我有一个通过API获取的JSONObject,就像这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 | { "wash_method": 305, "fragrance": 1156, "sweater_type": 260, "dial_color": 66475, "charger_type": 2581, "pen_drives_interface": 563, "watches_type": 66475, "processor": 3270, "frame_material": 1211, "dial_shape": 66475, "os": 3308 } |
我想根据值对它进行排序以获得这样的结果:
您会看到前3个项目的价值相似,因此可以以任何方式进行排列。我该怎么做? (有没有办法以最小的复杂度来做到这一点?)
这是您的问题的解决方案。希望对您有所帮助。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import org.json.JSONException; import org.json.JSONObject; import java.util.*; public class sample { private static class MyModel { String key; int value; MyModel(String key, int value) { this.key = key; this.value = value; } } public static void main(String[] args) { List<MyModel> list = new ArrayList<>(); try { //here is you json object JSONObject obj = new JSONObject(); obj.put("name1", 29); obj.put("name2", 26); obj.put("name3", 29); obj.put("name4", 27); //parsing your json Iterator< ? > keys = obj.keys(); MyModel objnew; while (keys.hasNext()) { String key = (String) keys.next(); objnew = new MyModel(key, obj.optInt(key)); list.add(objnew); } //sorting the values Collections.sort(list, new Comparator<MyModel>() { @Override public int compare(MyModel o1, MyModel o2) { return Integer.compare(o2.value, o1.value); } }); //print out put for (MyModel m : list) { System.out.println(m.key +" :" + m.value); } } catch (JSONException e) { e.printStackTrace(); } } |
} ??
尝试实现此功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | public class SomeClass { /** * Sorts the given JSONObject * * @param jsonObject * @return jsonObject sorted in descending order * @throws JSONException */ public JSONObject sortJSONObject(JSONObject jsonObject) throws JSONException { ArrayList<SomeChildClass> alstSomeChildClass = new ArrayList<>(); Iterator<String> keys = jsonObject.keys(); while (keys.hasNext()) { SomeChildClass someChildClass = new SomeChildClass(); someChildClass.key = keys.next(); someChildClass.value = jsonObject.getString(someChildClass.key); alstSomeChildClass.add(someChildClass); } // Sort the Collection Collections.sort(alstSomeChildClass); JSONObject sortedJsonObject = new JSONObject(); for (SomeChildClass someChildClass : alstSomeChildClass) sortedJsonObject.put(someChildClass.key, someChildClass.value); return sortedJsonObject; } private class SomeChildClass implements Comparable<SomeChildClass> { private String key; private String value; @Override public int compareTo(@NonNull SomeChildClass target) { int currentVal = Integer.parseInt(value); int targetVal = Integer.parseInt(target.value); return (currentVal < targetVal) ? 1 : ((currentVal == targetVal) ? 0 : -1); } } } |
UPDATE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | String json ="{\ " + " "wash_method": 305,\ " + " "fragrance": 1156,\ " + " "sweater_type": 260,\ " + " "dial_color": 66475,\ " + " "charger_type": 2581,\ " + " "pen_drives_interface": 563,\ " + " "watches_type": 66475,\ " + " "processor": 3270,\ " + " "frame_material": 1211,\ " + " "dial_shape": 66475,\ " + " "os": 3308\ " + " }"; try { JSONObject object = new SomeClass().sortJSONObject(new JSONObject(json)); Log.e("JSON", object.toString()); } catch (JSONException e) { e.printStackTrace(); } |