Author
Sorting wih Comparator dont work ?
Frank Jacobsen
Ranch Hand
Joined: May 17, 2002
Posts: 335
posted Jan 23, 2009 08:21:42
0
I have a class that sorts with comprator, but i dosent work !
Any good ideas, look at my code ?
Is there a better way to sort my list with objects ?
Daesung Park
Ranch Hand
Joined: Mar 22, 2007
Posts: 68
At a glance, creating a new instance in line 60 is problematic.
There are many ways to implement it correctly.
One solutin is to remove "implements Comparator" from TodaysHighAndLowBackingBean, and
Daesung Park
BLOG
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
Daesung Park wrote: At a glance, creating a new instance in line 60 is problematic.
Although it is unnecessary, it shouldn't create any problems. And replacing it with will work just as fine.
Frank, what is the value of navToDay1 and navToDay2? Are you sure those values are correct?
You might also want to consider using
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Frank Jacobsen
Ranch Hand
Joined: May 17, 2002
Posts: 335
posted Jan 23, 2009 09:54:32
0
Arrays.sort(fundArray, this); i will try this monday when i back on work....
The value are doubles and contains in the range -8.00 and 10.00
I get a List back in the fundDao.getAllFunds(), i dont want to implement the Comparable in fundDao.....
Is there a more effective way to do this, in sted off looping true the list, and put it into a Array and sort the Array ?
Frank
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
Frank Jacobsen wrote: Is there a more effective way to do this, in sted off looping true the list, and put it into a Array and sort the Array ?
I'm not sure what this question really means. There's Collections.sort(List, Comparator) which sorts a List -- so if you used that, you wouldn't have to copy the List to an array. You could just sort it in place. Or there's List.toArray() which produces an array from a List -- so if you used that, you wouldn't have to write your own code to copy the List to an array.
Frank Jacobsen
Ranch Hand
Joined: May 17, 2002
Posts: 335
posted Jan 23, 2009 12:11:34
0
So you mean that this will work:
this is implementing Comparator
this.funds is a list.
// see the above code
Frank
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
It should work. Of course you would have to change your method to return a List instead of an array of Funds.
Frank Jacobsen
Ranch Hand
Joined: May 17, 2002
Posts: 335
posted Jan 23, 2009 12:29:21
0
Thanks a LOT
subject: Sorting wih Comparator dont work ?