strange results of Double.MIN_VALUE is collection sort



A strange results of some simple code:

The code:

ArrayList al = new ArrayList();
al.add("-3.5");
al.add("+5");
al.add("+myinf");
al.add("-myinf");
al.add("+10");
System.out.println(al.toString());
Collections.sort(al, new Comparator()
{
public int compare(Object o1, Object o2)
{
Double D1 = null;
if ( ((String) o1).compareTo("-myinf") ==0)
D1 = new Double(Double.MIN_VALUE);
else
if ( ((String) o1).compareTo("+myinf") ==0)
D1 = new Double(Double.MAX_VALUE);
else
D1 = new Double((String) o1);
//
Double D2 = null;
if ( ((String) o2).compareTo("-myinf") ==0)
D2 = new Double(Double.MIN_VALUE);
else
if ( ((String) o2).compareTo("+myinf") ==0)
D2 = new Double(Double.MAX_VALUE);
else
D2 = new Double((String) o2);
//
return D1.compareTo(D2);
}
});
System.out.println(al.toString());


The output:
[-3.5, +5, +myinf, -myinf, +10]
[-3.5, -myinf, +5, +10, +myinf]

Any idea why -3.5 is sorted before (smalller than) -myinf?

.



Relevant Pages

  • Re: how to return Comparator values
    ... public int compare{ ... positive or zero. ... public Double getAge() ... A name Comparator would be similar, but use a different implementation of the comparemethod, one based on getNameinstead of getAge ...
    (comp.lang.java.help)
  • sorting an ArrayList by x or y value
    ... program I have to write that needs to use a Comparator. ... public int compare ... double pee = p.getX; ... double kyew = q.getX; ...
    (comp.lang.java.help)
  • Re: sorting an ArrayList by x or y value
    ... program I have to write that needs to use a Comparator. ... public int compare{ ... double pee = p.getX; ... double kyew = q.getX; ...
    (comp.lang.java.help)
  • Re: Sentai Groups
    ... As I recall, in order from the front, it was black, red, khaki (strange ... By the time the ATV came by, I was back paying attention ... Prev by Date: ...
    (rec.arts.anime.misc)
  • Re: Can you help me understand this?
    ... I know the intention is to provide your own comparator so that your list can be sorted by using it. ... public String firstName, lastName; ... public int compare{ ... int lastNameCompareResult = o1.lastName.compareTo; ...
    (comp.lang.java.programmer)