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

后端 未结 4 1547
花落未央
花落未央 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:59

    Using java8 there is easy way to do this:

    public List getSortedPlayerList(List playerList) {
        return playerList.stream().sorted(Comparator.comparing(PlayerStats::getPlayerLastName).thenComparing(PlayerStats::getPlayerFirstName)).collect(Collectors.toList());
    }
    

提交回复
热议问题