Home | History | Annotate | Download | only in table
      1 package autotest.common.table;
      2 
      3 import autotest.common.table.DataSource.SortSpec;
      4 
      5 import com.google.gwt.json.client.JSONObject;
      6 
      7 import java.util.Comparator;
      8 
      9 public class JSONObjectComparator implements Comparator<JSONObject> {
     10     SortSpec[] sortSpecs;
     11 
     12     public JSONObjectComparator(SortSpec[] specs) {
     13         sortSpecs = new SortSpec[specs.length];
     14         System.arraycopy(specs, 0, sortSpecs, 0, specs.length);
     15     }
     16 
     17     public int compare(JSONObject arg0, JSONObject arg1) {
     18         int compareValue = 0;
     19         for (SortSpec sortSpec : sortSpecs) {
     20             String key0 = arg0.get(sortSpec.getField()).toString().toLowerCase();
     21             String key1 = arg1.get(sortSpec.getField()).toString().toLowerCase();
     22             compareValue = key0.compareTo(key1) * sortSpec.getDirectionMultiplier();
     23             if (compareValue != 0) {
     24                 return compareValue;
     25             }
     26         }
     27 
     28         // the given sort keys were all equal, but we'll ensure we're consistent with
     29         // JSONObject.equals()
     30         if (arg0.equals(arg1)) {
     31             return 0;
     32         }
     33         // arbitrary (but consistent) ordering in this case
     34         return arg0.hashCode() > arg1.hashCode() ? 1 : -1;
     35     }
     36 }
     37