K
Kevin
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?
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?