I have an arrayList of different types of players based on sports. I need to sort the list of players in the arrayList by last name to start. If 2 players have the same la
Petar's answer is correct, just two remarks:
List
instead of ArrayList
as method argument, as the interface is more general, and the method will work even if you change to another List
type (like LinkedList
... ) laterAn improved version:
//the place where you define the List
List playerList = new ArrayList();
public static void sortPlayers(List playerList) {
Collections.sort(playerList, new Comparator() {
public int compare(PlayerStats p1, PlayerStats p2) {
int res = p1.getPlayerLastName().compareToIgnoreCase(p2.getPlayerLastName());
if (res != 0)
return res;
return p1.getPlayerFirstName().compareToIgnoreCase(p2.getPlayerFirstName())
}
});
}