Sorting Objects.

Whats the best/easyiest/efficient!! etc way to sort through and find the object with the highest score?

heres my the constructor for the object class,

public Candidates(String LastName,String FirstName, String PartyName,double Votes ){

heres the sortin algorithim im tryin to use.

public Candidates FindElected(){

int i,j;

Candidates aCandidate, bCandidate, tempCandidate;

for (i=1; i<CandidateVector.size(); i++){

for (j=0; j><CandidateVector.size()-i; j++){

aCandidate = (Candidates)CandidateVector.elementAt(j);

bCandidate = (Candidates)CandidateVector.elementAt(j+1);

if (aCandidate.getVotes() > bCandidate.getVotes()){// compare the two neighbors

tempCandidate =new Candidates();

tempCandidate.equals(aCandidate);/* swap a[j] and a[j+1]

*/

CandidateVector.insertsetElementAt((Candidates)bCandidate,(int)j);

CandidateVector.setElementAt((Candidates)tempCandidate,(int)j+1);

}// end of if

}// end of second for

}// end of first for

int index;

for (index = 0; index < CandidateVector.size(); index++ )

{

aCandidate =(Candidates)CandidateVector.elementAt(index);

System.out.println("Candidate " + index +"" + aCandidate.getLastName());

}

return (Candidates)CandidateVector.lastElement();

The results i get are either wrong, null or exception is thrown.

I think Im on the right track though

Any suggestions.

Ass always cheers!

[2555 byte] By [hommer] at [2007-9-26 19:16:47]
# 1

Have your class implement Comparable. Use the value returned by getVotes() to resolve compareTo(), like:

public int compareTo(Canidate canidate) {

int mine = this.getVotes();

int his = canidate.getVotes();

if (mine == his)

return 0;

if (mine > his)

return 1;

else

return -1;

}

Once you implement Comparable, you can use the Collections API to sort instances of the class.

rvflannery at 2007-7-3 11:23:48 > top of Java-index,Archived Forums,Java Programming...
# 2
How does this work if you have many objects to compare and dont know how many you will have to sort through each time?
hommer at 2007-7-3 11:23:48 > top of Java-index,Archived Forums,Java Programming...
# 3
Cheers for replying
hommer at 2007-7-3 11:23:48 > top of Java-index,Archived Forums,Java Programming...
# 4

You don't need to implement your own sorting algorithm, as the JDK already contains a sort() method in class java.util.Collections.

You can use it to sort a java.util.List. So to sort your Candidates objects, put them in a List, write a comparator (i.e., an object that implements the java.util.Comparator interface) and call Collections.sort().

See the API documentation.

Jesper

jesper1 at 2007-7-3 11:23:48 > top of Java-index,Archived Forums,Java Programming...
# 5

> How does this work if you have many objects to compare

> and dont know how many you will have to sort through

> each time?

It does not matter if you do or do not know how many Objects will be included in the Collection. If, for example, a dynamically-sized List of your objects is returned by getAllCanidates (say, as a Vector), you would just do,

List canidates = getAllCanidates();

Collections.sort(canidates);

rvflannery at 2007-7-3 11:23:48 > top of Java-index,Archived Forums,Java Programming...