edu.stanford.nlp.util
Class EntryValueComparator

java.lang.Object
  |
  +--edu.stanford.nlp.util.EntryValueComparator
All Implemented Interfaces:
Comparator

public class EntryValueComparator
extends Object
implements Comparator

Comparator for values of Map entries. Allows sorting in normal or reversed order. Values must implement Comparable to be used.

Example use (sorts map of counts with highest first):

 Map counts = ... // Object key -> Integer/Double count
 List entries=new ArrayList(counts.entrySet());
 Collections.sort(entries,new EntryValueComparator(false));
 

Author:
Joseph Smarr (jsmarr@stanford.edu)

Constructor Summary
EntryValueComparator()
          Constructs a new EntryValueComparator using ascending (normal) order that works on Map.Entry objects.
EntryValueComparator(boolean ascending)
          Constructs a new EntryValueComparator that will sort in the given order and works on Map.Entry objects.
EntryValueComparator(Map m)
          Constructs a new EntryValueComparator that will sort keys for the given Map in ascending (normal) order.
EntryValueComparator(Map m, boolean ascending)
          Constructs a new EmptyValueComparator to sort keys or entries of the given map in the given order.
 
Method Summary
 int compare(Object o1, Object o2)
          Compares the values of the two given Map.Entry objects in the given order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

EntryValueComparator

public EntryValueComparator()
Constructs a new EntryValueComparator using ascending (normal) order that works on Map.Entry objects.


EntryValueComparator

public EntryValueComparator(boolean ascending)
Constructs a new EntryValueComparator that will sort in the given order and works on Map.Entry objects.


EntryValueComparator

public EntryValueComparator(Map m)
Constructs a new EntryValueComparator that will sort keys for the given Map in ascending (normal) order. It will also sort Map.Entry objects.


EntryValueComparator

public EntryValueComparator(Map m,
                            boolean ascending)
Constructs a new EmptyValueComparator to sort keys or entries of the given map in the given order. If m is non-null, this Comparator can be used to sort its keySet() as well as its entrySet(). Otherwise it can only be used on the entries, since there's no way to get the value for a given key.

Parameters:
m - Map whose keys are to be sorted, or null if Map.Entry objects will be sorted.
ascending - whether to sort in ascending (normal) order or descending (reverse) order. Ascending order is alphabetical, descending order puts higher numbers first.
Method Detail

compare

public int compare(Object o1,
                   Object o2)
Compares the values of the two given Map.Entry objects in the given order. Returns 0 if either object is not a Map.Entry or if their values cannot be compared.

Specified by:
compare in interface Comparator


Stanford NLP Group