28
|
1 package de.mpiwg.itgroup.annotations.restlet.utils;
|
|
2
|
|
3 import java.util.Comparator;
|
31
|
4 import java.util.List;
|
28
|
5
|
|
6 import org.json.JSONException;
|
|
7 import org.json.JSONObject;
|
|
8
|
|
9 public class JSONObjectComparator implements Comparator<JSONObject>{
|
|
10
|
|
11 private String attributeToSort;
|
|
12 public JSONObjectComparator(String attribute){
|
|
13 this.attributeToSort=attribute;
|
|
14 }
|
|
15
|
|
16 public int compare( JSONObject a, JSONObject b ) {
|
|
17 // je quadratischer, desto grösser
|
|
18 String sortA;
|
|
19 try {
|
|
20 sortA = a.getString(attributeToSort);
|
|
21 } catch (JSONException e) {
|
|
22 sortA ="";
|
|
23 }
|
|
24 String sortB;
|
|
25 try {
|
|
26 sortB = b.getString(attributeToSort);
|
|
27 } catch (JSONException e) {
|
|
28 sortB="";
|
|
29 }
|
|
30
|
|
31
|
|
32 return sortA.compareToIgnoreCase(sortB);
|
|
33 }
|
|
34
|
31
|
35 /**
|
|
36 * Sortiere array nach einem Parameter in den Annotationen
|
|
37 * @param results
|
|
38 * @return
|
|
39 */
|
|
40 public static void sortAnnotations(List<JSONObject> results,String attribute) {
|
|
41 JSONObjectComparator comp = new JSONObjectComparator(attribute);
|
|
42
|
|
43
|
|
44
|
|
45 java.util.Collections.sort( results, comp);
|
|
46
|
|
47
|
|
48
|
|
49 }
|
28
|
50
|
|
51 } |