List of usage examples for org.jfree.data KeyedValueComparatorType BY_KEY
KeyedValueComparatorType BY_KEY
To view the source code for org.jfree.data KeyedValueComparatorType BY_KEY.
Click Source Link
From source file:org.jfree.data.KeyedValueComparator.java
/** * Compares two {@link KeyedValue} instances and returns an * <code>int</code> that indicates the relative order of the two objects. * * @param o1 object 1./*from w w w.j a v a 2 s. c om*/ * @param o2 object 2. * * @return An int indicating the relative order of the objects. */ @Override public int compare(Object o1, Object o2) { if (o2 == null) { return -1; } if (o1 == null) { return 1; } int result; KeyedValue kv1 = (KeyedValue) o1; KeyedValue kv2 = (KeyedValue) o2; if (this.type == KeyedValueComparatorType.BY_KEY) { if (this.order.equals(SortOrder.ASCENDING)) { result = kv1.getKey().compareTo(kv2.getKey()); } else if (this.order.equals(SortOrder.DESCENDING)) { result = kv2.getKey().compareTo(kv1.getKey()); } else { throw new IllegalArgumentException("Unrecognised sort order."); } } else if (this.type == KeyedValueComparatorType.BY_VALUE) { Number n1 = kv1.getValue(); Number n2 = kv2.getValue(); if (n2 == null) { return -1; } if (n1 == null) { return 1; } double d1 = n1.doubleValue(); double d2 = n2.doubleValue(); if (this.order.equals(SortOrder.ASCENDING)) { if (d1 > d2) { result = 1; } else if (d1 < d2) { result = -1; } else { result = 0; } } else if (this.order.equals(SortOrder.DESCENDING)) { if (d1 > d2) { result = -1; } else if (d1 < d2) { result = 1; } else { result = 0; } } else { throw new IllegalArgumentException("Unrecognised sort order."); } } else { throw new IllegalArgumentException("Unrecognised type."); } return result; }
From source file:org.jfree.data.DefaultKeyedValues.java
/** * Sorts the items in the list by key.// ww w . ja v a 2s. c o m * * @param order the sort order (<code>null</code> not permitted). */ public void sortByKeys(SortOrder order) { final int size = this.keys.size(); final DefaultKeyedValue[] data = new DefaultKeyedValue[size]; for (int i = 0; i < size; i++) { data[i] = new DefaultKeyedValue((Comparable) this.keys.get(i), (Number) this.values.get(i)); } Comparator comparator = new KeyedValueComparator(KeyedValueComparatorType.BY_KEY, order); Arrays.sort(data, comparator); clear(); for (int i = 0; i < data.length; i++) { final DefaultKeyedValue value = data[i]; addValue(value.getKey(), value.getValue()); } }