【问题标题】:Sort an ArrayList of Objects by last name, then first name按姓氏排序对象的 ArrayList,然后是名字
【发布时间】:2011-07-16 21:08:08
【问题描述】:

我有一个基于运动的不同类型球员的数组列表。我需要按姓氏对arrayList 中的玩家列表进行排序才能开始。如果 2 名玩家的姓氏相同,则需要按名字对这 2 名玩家进行排序。 示例:格式化姓氏名

Williams Robert
Phillips Warren
Doe John
Phillips Mark

输出应该是

Doe John
Phillips Mark
Phillips Warren
Williams Robert

我现在只按第一个或最后一个排序。我的代码中的最后一个 atm 有它。

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

    }
}

【问题讨论】:

    标签: java


    【解决方案1】:

    将比较器更改为:

    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 为您完成所有工作
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      使用 java8 有一个简单的方法来做到这一点:

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

      【讨论】:

        【解决方案4】:
            //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]
        

        【讨论】:

          猜你喜欢
          • 2019-05-06
          • 1970-01-01
          • 2020-11-03
          • 1970-01-01
          • 1970-01-01
          • 2010-10-26
          • 1970-01-01
          • 2022-01-14
          • 1970-01-01
          相关资源
          最近更新 更多