我有一个通过API获得的JSONObject。 就像这样:
{
"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
}
我想根据值对此进行排序,以获得如下结果:
{
"dial_shape": 66475,
"dial_color": 66475,
"watches_type": 66475,
"os": 3308,
"processor": 3270,
"charger_type": 2581,
"frame_material": 1211,
"fragrance": 1156,
"pen_drives_interface": 563,
"wash_method": 305,
"sweater_type": 260,
}
正如您所看到的,前三项具有相似的价值,因此可以以任何方式进行排列 我怎样才能做到这一点? (有没有办法以最小的复杂性做到这一点?)
答案 0 :(得分:1)
以下是您的问题的解决方案。我希望这会有所帮助。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Image</h2>
<p>The .img-responsive class makes the image scale nicely to the parent element (resize the browser window to see the effect):</p>
<img class="img-responsive" src="http://placehold.it/350x150" alt="Chania" width="460" height="345">
</div>
</body>
</html>
}
答案 1 :(得分:0)
尝试实施此
int[] col = YourClass.getTableSortedColumns(jTable);
for(int i = 0;i<col.length;i++){
if(col[i]>=0){
String orientation = "";
switch(YourClass.getTableSortedOrientation(jTable, col[i])){
case 1:
orientation = "Ascending";
break;
case 0:
orientation = "Unsorted";
break;
case -1:
orientation = "Descending";
break;
}
System.out.println("index "+col[i]+" "+orientation);
}
}
<强>更新强>
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);
}
}
}