【问题标题】:How to sort list by POJO property in descending order?如何按 POJO 属性按降序对列表进行排序?
【发布时间】:2015-08-18 23:50:36
【问题描述】:

我有这样的POJO班学生

class Student
{
    private int score;
    private String FirstName;
    //Getters and setters .................
}

我正在创建这样的 ArrayList

public static void main(String[] args)
{
    List<Student> al_students= new ArrayList<Student>();
    Student s1= new Student();
    s1.setScore(90);
    s1.setFirstName("abc");
    al_students.add(s1);

    Student s2= new Student();
    s2.setScore(95);
    s2.setFirstName("def");
    al_students.add(s2);

    Student s3= new Student();
    s3.setScore(85);
    s3.setFirstName("xyz");
    al_students.add(s3);
}

现在我想根据分数按降序对它进行排序,即
输出

1)def      95
2)abc      90
3)xyz      85

【问题讨论】:

标签: java list sorting


【解决方案1】:

如果您使用的是 Java 8,那么您的代码可能如下所示

al_students.sort(Comparator.comparingInt(Student::getScore).reversed());

【讨论】:

    【解决方案2】:

    几乎整个问题都被其他人回答了......

    由于有 2 个选项,使用 Comparable 接口或 Comparator 接口, 我希望你去this post 解释什么时候使用 Comparable 以及什么时候使用 Comparator。

    这对你真的很有帮助。

    【讨论】:

      【解决方案3】:

      您可以使用自定义Comparator。

      这是一个完整的例子(不包括进口):

      public class Main {
      
          // main method setting up and printing students
          public static void main(String[] args) {
              List<Student> students = new ArrayList<Student>();
              Student s1 = new Student();
              s1.setScore(90);
              s1.setFirstName("abc");
              students.add(s1);
      
              Student s2 = new Student();
              s2.setScore(95);
              s2.setFirstName("def");
              students.add(s2);
      
              Student s3 = new Student();
              s3.setScore(85);
              s3.setFirstName("xyz");
              students.add(s1);
              System.out.printf("Unordered: %s%n", students);
              // sorting using anonymous Comparator
              Collections.sort(students, new Comparator<Student>() {
                  public int compare(Student s1, Student s2) {
                      // notice the cast to (Integer) to invoke compareTo
                      return ((Integer)s1.getScore()).compareTo(s2.getScore());
                  }
              });
              System.out.printf("Ordered: %s%n", students);
          }
          // Student class
          static class Student {
              private int score;
              private String firstName;
              // boring stuff
              public int getScore() {
                  return score;
              }
      
              public void setScore(int score) {
                  this.score = score;
              }
      
              public String getFirstName() {
                  return firstName;
              }
      
              public void setFirstName(String name) {
                  this.firstName = name;
              }
              // for printing
              @Override
              public String toString() {
                  return String.format("Student \"%s\" with score: %d%n", firstName,
                          score);
              }
          }
      }
      

      输出

      Unordered: [Student "abc" with score: 90
      , Student "def" with score: 95
      , Student "abc" with score: 90
      ]
      Ordered: [Student "abc" with score: 90
      , Student "abc" with score: 90
      , Student "def" with score: 95
      ]
      

      注意

      正如其他人提到的,如果唯一(或默认)排序将按分数排序,您也可以在您的 Student 班级中 implement Comparable&lt;Student&gt;。#

      第二次修改

      为了按降序排序,您可以将Comparator 中的return 语句替换为以下内容:

      return ((Integer)s2.getScore()).compareTo(s1.getScore());
      

      感谢 programminglover 发现这一点/为错误地拒绝编辑表示歉意!

      【讨论】:

        【解决方案4】:

        看,有这个逻辑。
        Student 是您创建的课程。如果您想允许其他类按照您自己的要求对自定义对象进行排序,您应该告诉它。如果您实现 Comparable 或 Comparator 接口并在您的自定义类中覆盖这些方法并在这些方法中指定您将如何进行比较,就会发生这种情况。

        【讨论】:

          【解决方案5】:

          使用Comparator:

              Collections.sort(al_students, new Comparator<Student>() {
                  @Override
                  public int compare(Student o1, Student o2) {
                      return Integer.compare(o2.getScore(), o1.getScore());
                  }           
              });
          

          或者,让 Student 实现 Comparable 接口:

          class Student implements Comparable<Student> {
              ...
              @Override
              public int compareTo(Student s) {
                  return Integer.compare(s.getScore(), getScore());
              }
          }
          

          那么你可以不使用比较器进行排序:

          Collections.sort(al_students);
          

          【讨论】:

          • 要比较int,您可以使用Integer.compare(int1, int2)。无需将int 转至Integer 即可调用compareTo。
          • 谢谢@Pshemo,我更习惯C#,你可以在int上调用.compareTo。
          • 我认为我的 JDK 6 版本不支持它,但是您的某些回答对我有所帮助,这就是为什么我要支持您并标记 @Mena 回答正确我非常感谢您的努力跨度>
          【解决方案6】:

          您可以编写自定义Comparator 来解决这个问题。

          【讨论】:

            猜你喜欢
            • 2022-01-06
            • 1970-01-01
            • 1970-01-01
            • 2015-10-19
            • 1970-01-01
            • 2018-05-03
            • 1970-01-01
            • 2016-10-18
            相关资源
            最近更新 更多