comparable

hi

im trying to implement the comparable interfact to print out information about people in aplhabetical order

iv tried to write the code and it compiles however the information is still not being printed out in alphabetical order

i would appreciate it if anyone could look at my code and tell me where im going wrong

thanks

person class

publicclass Person

{

public String surname, firstname;

publicint age, weight;

publicdouble height;

public Person()

{

}

public Person(String firstname, String surname,int age,double height,int weight)

{

this.firstname = firstname;

this.surname = surname;

this.age = age;

this.height = height;

this.weight = weight;

}

public String getFirstname()

{

return firstname;

}

public String getSurname()

{

return surname;

}

publicint getAge()

{

return age;

}

publicdouble height()

{

return height;

}

publicint weight()

{

return weight;

}

}

[b]playerclass[/b]

[code]

publicclass Playerextends Personimplements Comparable<Player>

{

publicint gamesplayed, runsscored, timesdismissed;

public Player(String firstname, String surname,int age,double height,int weight,int gamesplayed,int runsscored,int timesdismissed)

{

super(firstname, surname, age, height, weight);

this.gamesplayed = gamesplayed;

this.runsscored = runsscored;

this.timesdismissed = timesdismissed;

}

publicint getGamesplayed()

{

return gamesplayed;

}

publicint getRunsscored()

{

return runsscored;

}

publicint getTimesdismissed()

{

return timesdismissed;

}

public Player(String firstname, String surname)

{

setfirstname(firstname);

setsurname(surname);

}

publicint compareTo(Player p)

{

return (getsurname() + getfirstname()).compareTo(p.getsurname() + p.getfirstname());

}

public String getfirstname()

{

return firstname;

}

publicvoid setfirstname(String firstname)

{

this.firstname = firstname;

}

public String getsurname()

{

return surname;

}

publicvoid setsurname(String surname)

{

this.surname = surname;

}

public String getDetails()

{

return"Firstname = " + firstname +" Surname = " + surname +" Age = " + age +" Height = " + height +"m Weight = " + weight +"kg Games Played = " + gamesplayed +" Runs Scored = " + runsscored +" Times Dismissed = " + timesdismissed;

}

}

[6124 byte] By [] at [2007-11-27 0:19:40]
# 1
is anybody able to offer any suggestions?thanks
at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 2
You should show how you use this comparable. Do you sort or something else?
Michael.Nazarov@sun.coma at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 3
> is anybody able to offer any suggestions?I can see a couple of reasons why it might not be working the way you expect. But instead of just dumping your code here for us to test, how about explaining in what way it doesn't work?
DrClapa at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 4

hi thanks for the feedback

i explained at the top what doesnt work.

i have an ArrayList in another class called Club

in the Club class i also has add and remove player methods

i also have a class called Main which is responsible for printing the information

below is the main code from class Club which has the ArrayList

if anybody is able to help i would very much appreciate it

thanks

public class Club

{

public String clubname;

ArrayList<Player> players = new ArrayList<Player>();

public Club(String clubname)

{

this.clubname = clubname;

}

public void addPlayer(String firstname, String surname, int age, double height, int weight, int gamesplayed, int runsscored, int timesdismissed)

{

players.add(new Player (firstname, surname, age, height, weight, gamesplayed, runsscored, timesdismissed));

}

DrClapa at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 5

> i explained at the top what doesnt work.

No, you didn't. You only said "the information is still not being printed out in alphabetical order". That is not an explanation. It could mean anything from the information not being sorted at all, to the information being printed out in reverse alphabetical order, to upper and lower cases being sorted separately (or not), or other minor flaws. Give us an example.

Edit: I don't see anywhere in your code where you actually sort anything. So if the problem is that the data remains in the sequence in which you added it, that could be the problem.

Message was edited by:

DrClap

DrClapa at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 6

oh i see, i appoligse

but as it stands it really doesnt seem to be doing anything

the information is printed out in the same order that it is created (as you mentioned)

therefore some sort code is required

is that something like

Collections.sort(players);?

because i tried this (and placed it under where the array list is defined)

and when attempting to compile it said <identifier> expected

is there something im missing?

thanks again for your help

DrClapa at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 7
any clues?thanks
DrClapa at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 8

> any clues?

>

> thanks

yeah, put the sort back in, and try to figure out why it gave you a compile time error... because that usually means you had bad syntax, not a bad algorithm. You WILL need to sort the list, or else choose a data structure that is always sorted (like a sorted set, i.e. a tree set).

- Adam

guitar_man_Fa at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 9
After all players have been added, call Collections.sort(), passing your array list. That needs to be done inside a method, not as a declaration.
dnathansona at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 10

I guess, to be more specific,

Adding elements to an array list puts the in the order they are added in. Period.

If you want them to be sorted, you must do so afterwards. Array list does not check to see if the type is comparable, or attempt to sort it without beind directed to, and it should not.

The idea of a list is for the creator to create any arbitrary order, and the list will preserve that order (as opposed to simply a collection, which does not guarantee that order is preserved). That's what a list is. If you want a different order, you must explicitly change that order.

- Adam

guitar_man_Fa at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 11
ahso the sort much be written in a sepeare methodhow would i write such a methodpublic orderList{Collections.sort(players);}something like that?
guitar_man_Fa at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 12

I guess you could do that. The point I was trying to make it that you are not declaring that you particular List is sorted. There's no such concept. It just sounded like you were trying to put an executable statement in a place where declaration should be.

Since you didn't post the code that is adding the players, I can't say exactly where your would call Collections.sort(). I was only saying that it must be inside *some* executable code block. Your method orderList() qualifies.

dnathansona at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 13
thanks again for the feedbackin reply 4 i posted the add player methodi dont know if this helps at allthanks
dnathansona at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...
# 14
Put your orderList() method in Club, and call it after you've added all the players. See if that works.
dnathansona at 2007-7-11 22:11:41 > top of Java-index,Java Essentials,New To Java...