This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes comparator help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "comparator help" Watch "comparator help" New topic
Author

comparator help

johnathan smith
Greenhorn

Joined: Mar 30, 2007
Posts: 21
hi
i have the following comparator class written to compare to players in a team by batting averages

i would now like to write a method called add to my Club class which passes a Player and a comparator hense writing a main program which which prints information about each player in the club in decreasing order of batting average?

Club class contains an array list and also add and remove player methods

how would this be achieved?
any hints or pseudo code would be much apprecaited
thanks

the comparator code
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
It would be better form only to have one return in the compare() method, but apart from that I think it ought to work. And you can change the 2nd if to an else-if
[ April 19, 2007: Message edited by: Campbell Ritchie ]
johnathan smith
Greenhorn

Joined: Mar 30, 2007
Posts: 21
thanks for the help

but i now want to write an add method to my club class which passes a Player and a Comparator and prints out the player information in decending order of batting average

how do i go about doing this?
any help is much appreciated

thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
I thought you already had an add method. And we discussed sorting a couple of days ago.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
I think you will find it much easier to add and sort separately. Apart from the fact that it is a bad idea to have a method which carries out two unrelated tasks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparator help
 
Similar Threads
naming arrayLists
problem with program hanging
Using Lists/ArrayLists, code doesn't work
Struggling with using methods from objects in another class
Please help, java program terminating unexpectedly without reason