public static <E> Comparator<E> toComparator(final Counter<E> counter, final boolean ascending, final boolean useMagnitude) {
    return new Comparator<E>() {
      public int compare(E o1, E o2) {
        if (ascending) {
          if (useMagnitude) {
            return Double.compare(Math.abs(counter.getCount(o1)), Math.abs(counter.getCount(o2)));
          } else {
            return Double.compare(counter.getCount(o1), counter.getCount(o2));
          }
        } else {
          // Descending
          if (useMagnitude) {
            return Double.compare(Math.abs(counter.getCount(o2)), Math.abs(counter.getCount(o1)));
          } else {
            return Double.compare(counter.getCount(o2), counter.getCount(o1));
          }
        }
      }
    };
  }
public static < E > Comparator < E > toComparator ( final Counter < E > counter , final boolean ascending , final boolean useMagnitude ) { return new Comparator < E > ( ) { public int compare ( E o1 , E o2 ) { if ( ascending ) { if ( useMagnitude ) { return Double . compare ( Math . abs ( counter . getCount ( o1 ) ) , Math . abs ( counter . getCount ( o2 ) ) ) ; } else { return Double . compare ( counter . getCount ( o1 ) , counter . getCount ( o2 ) ) ; } } else { if ( useMagnitude ) { return Double . compare ( Math . abs ( counter . getCount ( o2 ) ) , Math . abs ( counter . getCount ( o1 ) ) ) ; } else { return Double . compare ( counter . getCount ( o2 ) , counter . getCount ( o1 ) ) ; } } } } ; }
Returns a comparator suitable for sorting this Counter's keys or entries by
their respective value or magnitude (by absolute value). If
<tt>ascending</tt> is true, smaller magnitudes will be returned first,
otherwise higher magnitudes will be returned first.
<p/>
Sample usage:

<pre>
Counter c = new Counter();
// add to the counter...
List biggestKeys = new ArrayList(c.keySet());
Collections.sort(biggestAbsKeys, Counters.comparator(c, false, true));
List smallestEntries = new ArrayList(c.entrySet());
Collections.sort(smallestEntries, Counters.comparator(c, true, false));
</pre>