Sorting an ArrayList of Objects by Last name and firstname in Java

后端 未结 4 1559
花落未央
花落未央 2021-01-03 00:12

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

4条回答
  •  悲哀的现实
    2021-01-03 00:40

    Change the comparator to:

                public int compare(Object o1, Object o2) {
                    PlayerStats p1 = (PlayerStats) o1;
                    PlayerStats p2 = (PlayerStats) o2;
                    int res =  p1.getPlayerLastName().compareToIgnoreCase(p2.getPlayerLastName());
                    if (res != 0)
                        return res;
                    return p1.getPlayerFirstName().compareToIgnoreCase(p2.getPlayerFirstName())
                }
    

提交回复
热议问题