package de.mpiwg.itgroup.annotations.restlet.utils; /* * #%L * AnnotationManager * %% * Copyright (C) 2012 - 2014 MPIWG Berlin * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. * * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% * Author: Dirk Wintergruen (dwinter@mpiwg-berlin.mpg.de) */ import java.util.Comparator; import java.util.List; import org.json.JSONException; import org.json.JSONObject; public class JSONObjectComparator implements Comparator{ private String attributeToSort; public JSONObjectComparator(String attribute){ this.attributeToSort=attribute; } public int compare( JSONObject a, JSONObject b ) { // je quadratischer, desto grösser String sortA; try { sortA = a.getString(attributeToSort); } catch (JSONException e) { sortA =""; } String sortB; try { sortB = b.getString(attributeToSort); } catch (JSONException e) { sortB=""; } return sortA.compareToIgnoreCase(sortB); } /** * Sortiere array nach einem Parameter in den Annotationen * @param results * @return */ public static void sortAnnotations(List results,String attribute) { JSONObjectComparator comp = new JSONObjectComparator(attribute); java.util.Collections.sort( results, comp); } }