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