Collections.sort包含多个字段

时间:2010-11-23 17:03:46

标签: java sorting collections

我有一个“报告”对象列表,其中包含三个字段(所有字符串类型) -

ReportKey
StudentNumber
School

我的排序代码就像 -

Collections.sort(reportList, new Comparator<Report>() {

@Override
public int compare(final Report record1, final Report record2) {
      return (record1.getReportKey() + record1.getStudentNumber() + record1.getSchool())                      
        .compareTo(record2.getReportKey() + record2.getStudentNumber() + record2.getSchool());
      }

});

由于某种原因,我没有排序顺序。建议在字段之间放置空格,但为什么呢?

您认为代码有什么问题吗?

13 个答案:

答案 0 :(得分:108)

  

您认为代码有什么问题吗?

是。在比较它们之前,为什么要将这三个字段一起添加?

我可能会这样做:(假设字段按照您希望对其进行排序的顺序)

@Override public int compare(final Report record1, final Report record2) {
    int c;
    c = record1.getReportKey().compareTo(record2.getReportKey());
    if (c == 0)
       c = record1.getStudentNumber().compareTo(record2.getStudentNumber());
    if (c == 0)
       c = record1.getSchool().compareTo(record2.getSchool());
    return c;
}

答案 1 :(得分:47)

(来自Ways to sort lists of objects in Java based on multiple fields

this gist

中的工作代码

使用Java 8 lambda(2019年4月10日添加)

Java 8很好地解决了这个问题(虽然Guava和Apache Commons可能仍然提供更多的灵活性):

Collections.sort(reportList, Comparator.comparing(Report::getReportKey)
            .thenComparing(Report::getStudentNumber)
            .thenComparing(Report::getSchool));

感谢@ gaoagong的answer below

凌乱而错综复杂:手工排序

Collections.sort(pizzas, new Comparator<Pizza>() {  
    @Override  
    public int compare(Pizza p1, Pizza p2) {  
        int sizeCmp = p1.size.compareTo(p2.size);  
        if (sizeCmp != 0) {  
            return sizeCmp;  
        }  
        int nrOfToppingsCmp = p1.nrOfToppings.compareTo(p2.nrOfToppings);  
        if (nrOfToppingsCmp != 0) {  
            return nrOfToppingsCmp;  
        }  
        return p1.name.compareTo(p2.name);  
    }  
});  

这需要大量的打字,维护并且容易出错。

反射方式:使用BeanComparator进行排序

ComparatorChain chain = new ComparatorChain(Arrays.asList(
   new BeanComparator("size"), 
   new BeanComparator("nrOfToppings"), 
   new BeanComparator("name")));

Collections.sort(pizzas, chain);  

显然这更简洁,但更容易出错,因为你通过使用字符串而没有直接引用字段(没有类型安全,自动重构)。现在,如果重命名一个字段,编译器甚至不会报告问题。此外,由于此解决方案使用反射,因此排序速度要慢得多。

到达目的地:使用Google Guava的ComparisonChain进行排序

Collections.sort(pizzas, new Comparator<Pizza>() {  
    @Override  
    public int compare(Pizza p1, Pizza p2) {  
        return ComparisonChain.start().compare(p1.size, p2.size).compare(p1.nrOfToppings, p2.nrOfToppings).compare(p1.name, p2.name).result();  
        // or in case the fields can be null:  
        /* 
        return ComparisonChain.start() 
           .compare(p1.size, p2.size, Ordering.natural().nullsLast()) 
           .compare(p1.nrOfToppings, p2.nrOfToppings, Ordering.natural().nullsLast()) 
           .compare(p1.name, p2.name, Ordering.natural().nullsLast()) 
           .result(); 
        */  
    }  
});  

这样做要好得多,但是对于最常见的用例需要一些样板代码:默认情况下,null值的值应该更少。对于null-fields,你必须向Guava提供一个额外的指令,在这种情况下该做什么。如果您想要执行特定的操作,这是一种灵活的机制,但通常您需要默认情况(即1,a,b,z,null)。

使用Apache Commons CompareToBuilder进行排序

Collections.sort(pizzas, new Comparator<Pizza>() {  
    @Override  
    public int compare(Pizza p1, Pizza p2) {  
        return new CompareToBuilder().append(p1.size, p2.size).append(p1.nrOfToppings, p2.nrOfToppings).append(p1.name, p2.name).toComparison();  
    }  
});  

与Guava的ComparisonChain一样,此库类可以在多个字段上轻松排序,但也定义了空值的默认行为(即.1,a,b,z,null)。但是,除非您提供自己的比较器,否则您也无法指定任何其他内容。

因此

最终,它归结为味道和灵活性的需求(Guava的ComparisonChain)与简洁的代码(Apache的CompareToBuilder)。

奖金方法

我找到了一个很好的解决方案,它在MultiComparator中按优先顺序on CodeReview组合了多个比较器:

class MultiComparator<T> implements Comparator<T> {
    private final List<Comparator<T>> comparators;

    public MultiComparator(List<Comparator<? super T>> comparators) {
        this.comparators = comparators;
    }

    public MultiComparator(Comparator<? super T>... comparators) {
        this(Arrays.asList(comparators));
    }

    public int compare(T o1, T o2) {
        for (Comparator<T> c : comparators) {
            int result = c.compare(o1, o2);
            if (result != 0) {
                return result;
            }
        }
        return 0;
    }

    public static <T> void sort(List<T> list, Comparator<? super T>... comparators) {
        Collections.sort(list, new MultiComparator<T>(comparators));
    }
}

Ofcourse Apache Commons Collections已经为此提供了一个实用工具:

ComparatorUtils.chainedComparator(comparatorCollection)

Collections.sort(list, ComparatorUtils.chainedComparator(comparators));

答案 2 :(得分:44)

我使用GuavaComparisonChain

制作比较器
public class ReportComparator implements Comparator<Report> {
  public int compare(Report r1, Report r2) {
    return ComparisonChain.start()
        .compare(r1.getReportKey(), r2.getReportKey())
        .compare(r1.getStudentNumber(), r2.getStudentNumber())
        .compare(r1.getSchool(), r2.getSchool())
        .result();
  }
}

答案 3 :(得分:17)

这是一个老问题所以我没有看到Java 8的等价物。以下是此特定案例的示例。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Compares multiple parts of the Report object.
 */
public class SimpleJava8ComparatorClass {

    public static void main(String[] args) {
        List<Report> reportList = new ArrayList<>();
        reportList.add(new Report("reportKey2", "studentNumber2", "school1"));
        reportList.add(new Report("reportKey4", "studentNumber4", "school6"));
        reportList.add(new Report("reportKey1", "studentNumber1", "school1"));
        reportList.add(new Report("reportKey3", "studentNumber2", "school4"));
        reportList.add(new Report("reportKey2", "studentNumber2", "school3"));

        System.out.println("pre-sorting");
        System.out.println(reportList);
        System.out.println();

        Collections.sort(reportList, Comparator.comparing(Report::getReportKey)
            .thenComparing(Report::getStudentNumber)
            .thenComparing(Report::getSchool));

        System.out.println("post-sorting");
        System.out.println(reportList);
    }

    private static class Report {

        private String reportKey;
        private String studentNumber;
        private String school;

        public Report(String reportKey, String studentNumber, String school) {
            this.reportKey = reportKey;
            this.studentNumber = studentNumber;
            this.school = school;
        }

        public String getReportKey() {
            return reportKey;
        }

        public void setReportKey(String reportKey) {
            this.reportKey = reportKey;
        }

        public String getStudentNumber() {
            return studentNumber;
        }

        public void setStudentNumber(String studentNumber) {
            this.studentNumber = studentNumber;
        }

        public String getSchool() {
            return school;
        }

        public void setSchool(String school) {
            this.school = school;
        }

        @Override
        public String toString() {
            return "Report{" +
                   "reportKey='" + reportKey + '\'' +
                   ", studentNumber='" + studentNumber + '\'' +
                   ", school='" + school + '\'' +
                   '}';
        }
    }
}

答案 4 :(得分:13)

如果你想按报告密钥排序,那么学生编号,然后是学校,你应该做这样的事情:

public class ReportComparator implements Comparator<Report>
{
    public int compare(Report r1, Report r2)
    {
        int result = r1.getReportKey().compareTo(r2.getReportKey());
        if (result != 0)
        {
            return result;
        }
        result = r1.getStudentNumber().compareTo(r2.getStudentNumber());
        if (result != 0)
        {
            return result;
        }
        return r1.getSchool().compareTo(r2.getSchool());
    }
}

这假设没有一个值可以为null,当然 - 如果你需要允许报告,报告密钥,学号或学校的空值,它会变得更复杂。

虽然你可以让字符串连接版本使用空格来工作,但如果你有奇怪的数据本身包含空格等,它仍会在奇怪的情况下失败。上面的代码是逻辑您想要的代码...首先按报告键进行比较,如果报告密钥相同,则只需考虑学号,等等。

答案 5 :(得分:5)

使用Java8中的多个字段进行排序

package com.java8.chapter1;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import static java.util.Comparator.*;



 public class Example1 {

    public static void main(String[] args) {
        List<Employee> empList = getEmpList();


        // Before Java 8 
        empList.sort(new Comparator<Employee>() {

            @Override
            public int compare(Employee o1, Employee o2) {
                int res = o1.getDesignation().compareTo(o2.getDesignation());
                if (res == 0) {
                    return o1.getSalary() > o2.getSalary() ? 1 : o1.getSalary() < o2.getSalary() ? -1 : 0;
                } else {
                    return res;
                }

            }
        });
        for (Employee emp : empList) {
            System.out.println(emp);
        }
        System.out.println("---------------------------------------------------------------------------");

        // In Java 8

        empList.sort(comparing(Employee::getDesignation).thenComparing(Employee::getSalary));
        empList.stream().forEach(System.out::println);

    }
    private static List<Employee> getEmpList() {
        return Arrays.asList(new Employee("Lakshman A", "Consultent", 450000),
                new Employee("Chaitra S", "Developer", 250000), new Employee("Manoj PVN", "Developer", 250000),
                new Employee("Ramesh R", "Developer", 280000), new Employee("Suresh S", "Developer", 270000),
                new Employee("Jaishree", "Opearations HR", 350000));
    }
}

class Employee {
    private String fullName;
    private String designation;
    private double salary;

    public Employee(String fullName, String designation, double salary) {
        super();
        this.fullName = fullName;
        this.designation = designation;
        this.salary = salary;
    }

    public String getFullName() {
        return fullName;
    }

    public String getDesignation() {
        return designation;
    }

    public double getSalary() {
        return salary;
    }

    @Override
    public String toString() {
        return "Employee [fullName=" + fullName + ", designation=" + designation + ", salary=" + salary + "]";
    }

}

答案 6 :(得分:4)

我建议使用Java 8 Lambda方法:

List<Report> reportList = new ArrayList<Report>();
reportList.sort(Comparator.comparing(Report::getRecord1).thenComparing(Report::getRecord2));

答案 7 :(得分:3)

如果您想先根据ReportKey进行排序,然后再根据学生编号进行排序,则需要比较每个字符串而不是连接它们。如果用空格填充字符串以使每个ReportKey具有相同的长度等等,那么您的方法可能会起作用,但这并不值得付出努力。相反,只需更改比较方法以比较ReportKeys,如果compareTo返回0,则尝试StudentNumber,然后尝试学校。

答案 8 :(得分:3)

如果StudentNumber是数字,则不会对数字进行排序,而是按字母数字排序。 不要指望

"2" < "11"

它将是:

"11" < "2"

答案 9 :(得分:3)

Comparator接口与JDK1.8中引入的方法一起使用:comparingthenComparing,或更具体的方法:comparingXXXthenComparingXXX

例如,如果我们要按人的ID排序,然后按年龄,然后按名称排序:

            Comparator<Person> comparator = Comparator.comparingLong(Person::getId)
                    .thenComparingInt(Person::getAge)
                    .thenComparing(Person::getName);
            personList.sort(comparator);

答案 10 :(得分:0)

这是一个完整的例子,比较一个对象中的两个字段,一个字符串和一个int,也使用Collat​​or进行排序。

public class Test {

    public static void main(String[] args) {

        Collator myCollator;
        myCollator = Collator.getInstance(Locale.US);

        List<Item> items = new ArrayList<Item>();

        items.add(new Item("costrels", 1039737, ""));
        items.add(new Item("Costs", 1570019, ""));
        items.add(new Item("costs", 310831, ""));
        items.add(new Item("costs", 310832, ""));

        Collections.sort(items, new Comparator<Item>() {
            @Override
            public int compare(final Item record1, final Item record2) {
                int c;
                //c = record1.item1.compareTo(record2.item1); //optional comparison without Collator                
                c = myCollator.compare(record1.item1, record2.item1);
                if (c == 0) 
                {
                    return record1.item2 < record2.item2 ? -1
                            :  record1.item2 > record2.item2 ? 1
                            : 0;
                }
                return c;
            }
        });     

        for (Item item : items)
        {
            System.out.println(item.item1);
            System.out.println(item.item2);
        }       

    }

    public static class Item
    {
        public String item1;
        public int item2;
        public String item3;

        public Item(String item1, int item2, String item3)
        {
            this.item1 = item1;
            this.item2 = item2;
            this.item3 = item3;
        }       
    }

}

输出:

costrels 1039737

成本 310831

成本 310832

费用 1570019

答案 11 :(得分:0)

上面的很多答案都有用单个比较器方法比较的字段,但实际上不起作用。尽管对于每个字段实现了不同的比较器,但还是有一些答案,我将其发布是因为我相信此示例将更加清晰和易于理解。

class Student{
    Integer bornYear;
    Integer bornMonth;
    Integer bornDay;
    public Student(int bornYear, int bornMonth, int bornDay) {

        this.bornYear = bornYear;
        this.bornMonth = bornMonth;
        this.bornDay = bornDay;
    }
    public Student(int bornYear, int bornMonth) {

        this.bornYear = bornYear;
        this.bornMonth = bornMonth;

    }
    public Student(int bornYear) {

        this.bornYear = bornYear;

    }
    public Integer getBornYear() {
        return bornYear;
    }
    public void setBornYear(int bornYear) {
        this.bornYear = bornYear;
    }
    public Integer getBornMonth() {
        return bornMonth;
    }
    public void setBornMonth(int bornMonth) {
        this.bornMonth = bornMonth;
    }
    public Integer getBornDay() {
        return bornDay;
    }
    public void setBornDay(int bornDay) {
        this.bornDay = bornDay;
    }
    @Override
    public String toString() {
        return "Student [bornYear=" + bornYear + ", bornMonth=" + bornMonth + ", bornDay=" + bornDay + "]";
    }


}
class TestClass
{       

    // Comparator problem in JAVA for sorting objects based on multiple fields 
    public static void main(String[] args)
    {
        int N,c;// Number of threads

        Student s1=new Student(2018,12);
        Student s2=new Student(2018,12);
        Student s3=new Student(2018,11);
        Student s4=new Student(2017,6);
        Student s5=new Student(2017,4);
        Student s6=new Student(2016,8);
        Student s7=new Student(2018);
        Student s8=new Student(2017,8);
        Student s9=new Student(2017,2);
        Student s10=new Student(2017,9);

        List<Student> studentList=new ArrayList<>();
        studentList.add(s1);
        studentList.add(s2);
        studentList.add(s3);
        studentList.add(s4);
        studentList.add(s5);
        studentList.add(s6);
        studentList.add(s7);
        studentList.add(s8);
        studentList.add(s9);
        studentList.add(s10);

        Comparator<Student> byMonth=new Comparator<Student>() {
            @Override
            public int compare(Student st1,Student st2) {
                if(st1.getBornMonth()!=null && st2.getBornMonth()!=null) {
                    return st2.getBornMonth()-st1.getBornMonth();
                }
                else if(st1.getBornMonth()!=null) {
                    return 1;
                }
                else {
                    return -1;
                }
        }};

        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student st1,Student st2) {
                return st2.getBornYear()-st1.getBornYear();
        }}.thenComparing(byMonth));

        System.out.println("The sorted students list in descending is"+Arrays.deepToString(studentList.toArray()));



    }

}

输出

降序排列的学生列表为[学生[bornYear = 2018,bornMonth = null,bornDay = null],学生[bornYear = 2018,bornMonth = 12,bornDay = null],学生[bornYear = 2018,bornMonth = 12 ,bornDay = null],学生[bornYear = 2018,bornMonth = 11,bornDay = null],学生[bornYear = 2017,bornMonth = 9,bornDay = null],学生[bornYear = 2017,bornMonth = 8,bornDay = null] ,学生[bornYear = 2017,bornMonth = 6,bornDay = null],学生[bornYear = 2017,bornMonth = 4,bornDay = null],学生[bornYear = 2017,bornMonth = 2,bornDay = null],学生[bornYear = 2016年,bornMonth = 8,bornDay = null]]

答案 12 :(得分:0)

我遇到了同样的问题,我需要一个使用配置文件的算法。通过这种方式,您可以使用由配置文件定义的多个字段(仅通过 List

  public static void test() {  
    // Associate your configName with your Comparator 
    Map<String, Comparator<DocumentDto>> map = new HashMap<>();
    map.put("id", new IdSort());
    map.put("createUser", new DocumentUserSort());
    map.put("documentType", new DocumentTypeSort());
    /**
      In your config.yml file, you'll have something like
      sortlist: 
        - documentType
        - createUser
        - id
    */
    List<String> config = new ArrayList<>();
    config.add("documentType");
    config.add("createUser");
    config.add("id");
    

    List<Comparator<DocumentDto>> sorts = new ArrayList<>();

    for (String comparator : config) {
        sorts.add(map.get(comparator));
    }

    // Begin creation of the list 
    DocumentDto d1 = new DocumentDto();
    d1.setDocumentType(new DocumentTypeDto());
    d1.getDocumentType().setCode("A");
    d1.setId(1);
    d1.setCreateUser("Djory");

    DocumentDto d2 = new DocumentDto();
    d2.setDocumentType(new DocumentTypeDto());
    d2.getDocumentType().setCode("A");
    d2.setId(2);
    d2.setCreateUser("Alex");

    DocumentDto d3 = new DocumentDto();
    d3.setDocumentType(new DocumentTypeDto());
    d3.getDocumentType().setCode("A");
    d3.setId(3);
    d3.setCreateUser("Djory");

    DocumentDto d4 = new DocumentDto();
    d4.setDocumentType(new DocumentTypeDto());
    d4.getDocumentType().setCode("A");
    d4.setId(4);
    d4.setCreateUser("Alex");

    DocumentDto d5 = new DocumentDto();
    d5.setDocumentType(new DocumentTypeDto());
    d5.getDocumentType().setCode("D");
    d5.setId(5);
    d5.setCreateUser("Djory");

    DocumentDto d6 = new DocumentDto();
    d6.setDocumentType(new DocumentTypeDto());
    d6.getDocumentType().setCode("B");
    d6.setId(6);
    d6.setCreateUser("Alex");

    DocumentDto d7 = new DocumentDto();
    d7.setDocumentType(new DocumentTypeDto());
    d7.getDocumentType().setCode("B");
    d7.setId(7);
    d7.setCreateUser("Alex");

    List<DocumentDto> documents = new ArrayList<>();

    documents.add(d1);
    documents.add(d2);
    documents.add(d3);
    documents.add(d4);
    documents.add(d5);
    documents.add(d6);
    documents.add(d7);
    // End creation of the list


    // The Sort 
    Stream<DocumentDto> docStream = documents.stream();

    // we need to reverse this list in order to sort by documentType first because stream are pull-based, last sorted() will have the priority
    Collections.reverse(sorts);

    for(Comparator<DocumentDto> entitySort : sorts){
        docStream = docStream.sorted(entitySort);
    }
    documents = docStream.collect(Collectors.toList());
    // documents has been sorted has you configured
    // in case of equality second sort will be used.

    System.out.println(documents);

  }

比较器对象非常简单。

public class IdSort implements Comparator<DocumentDto> {

    @Override
    public int compare(DocumentDto o1, DocumentDto o2) {
        return o1.getId().compareTo(o2.getId());
    }
}
public class DocumentUserSort implements Comparator<DocumentDto> {

    @Override
    public int compare(DocumentDto o1, DocumentDto o2) {
        return o1.getCreateUser().compareTo(o2.getCreateUser());
    }
}
public class DocumentTypeSort implements Comparator<DocumentDto> {

    @Override
    public int compare(DocumentDto o1, DocumentDto o2) {
        return o1.getDocumentType().getCode().compareTo(o2.getDocumentType().getCode());
    }
}

结论:这种方法效率不高,但您可以通过这种方式使用文件配置创建通用排序。