I am trying to fix my sort method. I am reading in from a txt file and trying to get them in alphabetic order.
I can't use the implements comparable then use collections.sort() because my instructor won't allow it.
//How I am trying to sort
public static void sortPlayers(ArrayList<BingoPlayer> players){
BingoPlayer temp;
int value = 0;
for(int i = 0; i < players.size()-1; i++)
{
value = compareTo(players.get(i), players.get(i + 1));
if(value < 0 || value == 0){
//continue;
}
else if(value > 0){
temp = players.get(i);
players.set(i, players.get(i + 1));
players.set(i + 1, temp);
}
}
}
//How am I comparing.
public static int compareTo(BingoPlayer player1, BingoPlayer player2){
if(player1.firstName.compareTo(player2.firstName) < 0)
return player1.firstName.compareTo((player2.firstName));
else if(player1.firstName.compareTo(player2.firstName) > 0)
return player1.firstName.compareTo((player2.firstName));
else
if (player1.lastName.compareTo(player2.lastName) < 0)
return player1.lastName.compareTo((player2.lastName));
else if (player1.lastName.compareTo(player2.lastName) > 0)
return player1.lastName.compareTo((player2.lastName));
else
return 0;
}
Here is the txt file I am reading from:
50.00
10
James,Smith,50.0
Michael,Smith,50.0
Robert,Smith,50.0
Maria,Garcia,50.0
David,Smith,50.0
Maria,Rodriguez,50.0
Mary,Smith,50.0
Maria,Hernandez,50.0
Maria,Martinez,50.0
James,Johnson,50.0