strange results of Double.MIN_VALUE is collection sort
- From: "Kevin" <kaidizhao@xxxxxxxxxxxx>
- Date: 26 Jul 2005 22:22:32 -0700
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?
.
- Follow-Ups:
- Re: strange results of Double.MIN_VALUE is collection sort
- From: Dale King
- Re: strange results of Double.MIN_VALUE is collection sort
- From: Roedy Green
- Re: strange results of Double.MIN_VALUE is collection sort
- From: Patricia Shanahan
- Re: strange results of Double.MIN_VALUE is collection sort
- Prev by Date: Re: button- can click more than once and data is getting inserted into the database repeatedly
- Next by Date: Re: strange results of Double.MIN_VALUE is collection sort
- Previous by thread: Need source code w/ lots of generics
- Next by thread: Re: strange results of Double.MIN_VALUE is collection sort
- Index(es):
Relevant Pages
|