source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/utils/JSONObjectComparator.java @ 70:2b1e6df5e21a

Last change on this file since 70:2b1e6df5e21a was 70:2b1e6df5e21a, checked in by casties, 10 years ago

added lgpl_v3 license information.

File size: 1.9 KB
Line 
1package de.mpiwg.itgroup.annotations.restlet.utils;
2
3/*
4 * #%L
5 * AnnotationManager
6 * %%
7 * Copyright (C) 2012 - 2014 MPIWG Berlin
8 * %%
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU General Lesser Public
20 * License along with this program.  If not, see
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22 * #L%
23 * Author: Dirk Wintergruen (dwinter@mpiwg-berlin.mpg.de)
24 */
25
26import java.util.Comparator;
27import java.util.List;
28
29import org.json.JSONException;
30import org.json.JSONObject;
31
32public class JSONObjectComparator implements Comparator<JSONObject>{
33   
34        private String attributeToSort;
35        public JSONObjectComparator(String attribute){
36                this.attributeToSort=attribute;
37        }
38       
39        public int compare( JSONObject a, JSONObject b ) {
40        // je quadratischer, desto grösser
41                String sortA;
42                try {
43                        sortA = a.getString(attributeToSort);
44                } catch (JSONException e) {
45                        sortA ="";
46                }
47                String sortB;
48                try {
49                        sortB = b.getString(attributeToSort);
50                } catch (JSONException e) {
51                        sortB="";
52                }
53       
54               
55        return sortA.compareToIgnoreCase(sortB);
56    }
57       
58        /**
59         * Sortiere array nach einem Parameter in den Annotationen
60         * @param results
61         * @return
62         */
63        public static void sortAnnotations(List<JSONObject> results,String attribute) {
64                JSONObjectComparator comp = new JSONObjectComparator(attribute);
65               
66               
67       
68                java.util.Collections.sort( results, comp);
69               
70               
71
72        }       
73       
74}
Note: See TracBrowser for help on using the repository browser.