[Biojava-dev] SortedMap comparator question

Michael L. Heuer heuermh@acm.org
Wed, 9 Oct 2002 17:22:59 -0400 (EDT)


Sorry for this offtopic question, but does anyone know if it is legal to
use a comparator that compares values stored in the map to sort keys in a
SortedMap?

Hmm .. I guess that might be hard to read.

Something like

class MySortedMap extends TreeMap implements SortedMap
{
  public MySortedMap()
  {
    super(new DoubleValueComparator());
  }

  private static class DoubleValueComparator
    implements Comparator
  {
    public int compare(Object o1, Object o2)
    {
      if (o1.equals(o2))
        return 0;

      double d1 = ((Double) MySortedMap.this.get(o1)).doubleValue();
      double d2 = ((Double) MySortedMap.this.get(o2)).doubleValue();

      return (int) (d1 - d2);
    }
  }
}

for a map of Objects -> Doubles.

   michael