按姓氏和名字对对象的ArrayList进行排序

7

我有一个基于不同运动类型的球员arrayList。我需要按照姓氏对球员列表进行排序。如果两个球员有相同的姓氏,则需要按照名字对这两个球员进行排序。 例如:格式为 姓 名字

Williams Robert
Phillips Warren
Doe John
Phillips Mark

输出结果应该为:
Doe John
Phillips Mark
Phillips Warren
Williams Robert

目前我只能按照名字的首字母或者姓氏进行排序。在我的代码中,我使用的是按照姓氏进行排序。

public static void sortPlayers(ArrayList playerList) {
    for (int i = 0; i < playerList.size(); i++) {
        for (int j = 0; j < playerList.size(); j++) {
            Collections.sort(playerList, new Comparator() {

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

    }
}
4个回答

19

将比较器更改为:

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())
}

此外,摆脱嵌套循环。Collections.sort会为您完成所有工作。 - dfb

5

Petar的答案是正确的,只有两点需要注意:

  • 使用List而不是ArrayList作为方法参数,因为接口更加通用,即使以后更改为另一个List类型(如LinkedList),该方法也会起作用
  • 使用泛型使您的代码更具类型安全性。

改进后的版本:

//the place where you define the List
List<PlayerStats> playerList = new ArrayList<PlayerStats>();


public static void sortPlayers(List<PlayerStats> playerList) {
   Collections.sort(playerList, new Comparator<PlayerStats>() {
       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())
       }
   });
}

5
使用Java8,有一种简单的方法可以实现这一点:
public List<PlayerStats> getSortedPlayerList(List<PlayerStats> playerList) {
    return playerList.stream().sorted(Comparator.comparing(PlayerStats::getPlayerLastName).thenComparing(PlayerStats::getPlayerFirstName)).collect(Collectors.toList());
}

2
    //requires java@8
    //class Person { String fName; String lName; int id}

    List<Person> list = new ArrayList<>();
    Person p1 = new Person();
    p1.setfName("a");
    p1.setlName("x");
    list.add(p1 );

    Person p4 = new Person();
    p4.setfName("b");
    p4.setlName("z");
    list.add(p4);

    Person p3 = new Person();
    p3.setfName("a");
    p3.setlName("z");
    list.add(p3);

    Person p2 = new Person();
    p2.setfName("a");
    p2.setlName("y");
    list.add(p2);

    //sort by a single field
    Collections.sort(list, (o1,o2) ->  o1.getfName().compareTo(o2.getfName()));

    //sort by multiple cascading comparator.
    Collections.sort(list, Comparator.comparing(Person::getfName).thenComparing(Person::getlName));
    list.forEach( System.out::println);

    //output
    //Person [fName=a, lName=x, id=null]
    //Person [fName=a, lName=y, id=null]
    //Person [fName=a, lName=z, id=null]
    //Person [fName=b, lName=z, id=null]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接