Package com.groiss.ds

Class JsonComparator

java.lang.Object
com.groiss.ds.JsonComparator
All Implemented Interfaces:
Comparator<JSONObject>

public class JsonComparator extends Object implements Comparator<JSONObject>
Compares json objects according to the passed attributes. If the attribute name starts with '-' this indicates descending order. If not or if it starts with a blank this indicates ascending order. Comparison is done as follows:
  1. if attribute holds Strings StringUtil.getCollator() will be used
  2. if attribute holds Comparable its compare-method will be used
  3. for all other cases the toString() result will be compared - again with StringUtil.getCollator()
  • Constructor Details

    • JsonComparator

      public JsonComparator(String attributes)
      Creates a new comparator object to sort according to the passed attributes
      Parameters:
      attributes - the name of the attributes by which sorting should be performed, e.g. "firstAttr,-secondAttr" means first sort ascending by 'firstAttr' and then (if necessary) sort descending by 'secondAttr'
    • JsonComparator

      public JsonComparator(String[] attributes)
      Creates a new comparator object to sort according to the passed attributes
      Parameters:
      attributes - the name of the attributes by which sorting should be performed, e.g. ["firstAttr","-secondAttr"] means first sort ascending by 'firstAttr' and then (if necessary) sort descending by 'secondAttr'
  • Method Details