1. Overview
1.概述
In this tutorial, we’ll discuss sorting objects in a List by date. Most of the sorting techniques or examples let the user sort a list alphabetically, but in this article, we’ll discuss how to do it with Date objects.
在本教程中,我们将讨论按日期对List中的对象进行排序。大多数排序技术或例子都让用户按字母顺序对列表进行排序,但在这篇文章中,我们将讨论如何用Date对象来进行排序。
We’ll look at using Java’s Comparator class for custom sorting our lists’ values.
我们将看看如何使用Java的Comparator类来对我们的列表值进行自定义排序。
2. Setup
2.设置
Let’s look at the Employee entity we’ll be using in this article:
让我们看看我们将在本文中使用的Employee实体。
public class Employee implements Comparable<Employee> {
private String name;
private Date joiningDate;
public Employee(String name, Date joiningDate) {
// ...
}
// standard getters and setters
}
We can notice that we have implemented a Comparable interface in the Employee class. This interface lets us define a strategy for comparing objects with other objects of the same type. This is used to sort the objects in their natural ordering form or defined by the compareTo() method.
我们可以注意到,我们在Employee类中实现了一个Comparable接口。这个接口让我们可以定义一个策略,将对象与其他相同类型的对象进行比较。这用于以自然排序的形式对对象进行排序,或者由compareTo()方法定义。
3. Sorting Using Comparable
3.使用Comparable进行排序
In Java, natural order refers to how we should sort primitives or objects in an array or collection. The sort() method in java.util.Arrays and java.util.Collections should be consistent and reflect the semantics of equality.
在Java中,自然顺序是指我们应该如何对数组或集合中的基元或对象进行排序。java.util.Arrays和java.util.Collections中的sort()方法应该是一致的,并且反映了平等的语义。
We’ll use this method for comparing the current object and the object passed as an argument:
我们将使用这个方法来比较当前对象和作为参数传递的对象。
public class Employee implements Comparable<Employee> {
// ...
@Override
public boolean equals(Object obj) {
return ((Employee) obj).getName().equals(getName());
}
@Override
public int compareTo(Employee employee) {
return getJoiningDate().compareTo(employee.getJoiningDate());
}
}
This compareTo() method will be comparing the current object with the object that is being sent as a parameter. In the above example, we compare the joining date of the current object with the passed Employee object.
这个compareTo()方法将比较当前对象和作为参数被发送的对象。在上面的例子中,我们比较了当前对象的加入日期和传递的Employee对象。
3.1. Sorting in Ascending Order
3.1.按升序排序
In most cases, the compareTo() method describes the logic for comparison among objects with natural sorting. Here, we compare the employee’s joining date field with other objects of the same type. Any two employees will return 0 if they have the same joining date:
在大多数情况下,compareTo()方法描述了自然排序的对象之间的比较逻辑。这里,我们将雇员的加入日期字段与其他相同类型的对象进行比较。任何两个雇员如果有相同的入职日期,将返回0。
@Test
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
Collections.sort(employees);
assertEquals(employees, employeesSortedByDateAsc);
}
Now, the Collections.sort(employees) will sort the employee list based on its joiningDate instead of its primary key or name. We can see the list is sorted by joiningDate of the employees – that now becomes a natural order for Employee class:
现在,Collections.sort(employees)将根据雇员列表的joiningDate而不是其主键或名字来排序。我们可以看到列表是按照雇员的加入日期来排序的–这现在成为雇员类的自然顺序。
[(Pearl,Tue Apr 27 23:30:47 IST 2021),
(Earl,Sun Feb 27 23:30:47 IST 2022),
(Steve,Sun Apr 17 23:30:47 IST 2022),
(John,Wed Apr 27 23:30:47 IST 2022)]
3.2. Sorting in Descending Order
3.2.按降序排序
The Collections.reverseOrder() method sort the objects but in the reverse order as imposed by the natural ordering. This returns a comparator that will perform the ordering in reverse. It’ll throw a NullPointerException when the object returns null on the comparison:
Collections.reverseOrder()方法对对象进行排序,但顺序与自然排序的顺序相反。这将返回一个比较器,它将以反向方式执行排序。当对象在比较中返回null时,它将抛出一个NullPointerException。
@Test
public void givenEmpList_SortEmpList_thenSortedListinDescOrder() {
Collections.sort(employees, Collections.reverseOrder());
assertEquals(employees, employeesSortedByDateDesc);
}
4. Sorting Using Comparator
4.使用Comparator进行排序
4.1. Sorting in Ascending Order
4.1.按升序排序
Let’s now use the Comparator interface implementation to sort our employee list. Here, we’ll pass an anonymous inner class parameter on the on-the-fly to the Collections.sort() API:
现在让我们使用Comparator接口实现来对我们的雇员列表进行排序。在这里,我们将向Collections.sort() API即时传递一个匿名的内部类参数。
@Test
public void givenEmpList_SortEmpList_thenCheckSortedList() {
Collections.sort(employees, new Comparator<Employee>() {
public int compare(Employee o1, Employee o2) {
return o1.getJoiningDate().compareTo(o2.getJoiningDate());
}
});
assertEquals(employees, employeesSortedByDateAsc);
}
We can also replace this syntax with the Java 8 Lambda Syntax that makes our code much smaller, as shown below:
我们也可以用Java 8 Lambda语法代替这种语法,使我们的代码更小,如下所示。
@Test
public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {
Collections.sort(employees, Comparator.comparing(Employee::getJoiningDate));
assertEquals(employees, employeesSortedByDateAsc);
}
The compare(arg1, arg2) method takes two arguments of the generic type and returns an integer. Since it is separated from the class definition, we can define a custom comparison based on different variables and entities. This is useful when we want to define a different custom sort for comparing the argument objects.
compare(arg1, arg2)方法接收两个通用类型的参数并返回一个整数。由于它与类的定义分离,我们可以根据不同的变量和实体定义一个自定义的比较。当我们想定义一个不同的自定义排序来比较参数对象时,这很有用。
4.2. Sorting in Descending Order
4.2.按降序排序
We can sort the given Employee list in descending order by reversing the employee object comparison, i.e., comparing Employee2 with Employee1. This will reverse the comparison and thus return the result in descending order:
我们可以通过反转雇员对象的比较,即比较Employee2和Employee1,将给定的Employee列表按降序排序。这将反转比较,从而以降序返回结果。
@Test
public void givenEmpList_SortEmpList_thenCheckSortedListDescV1() {
Collections.sort(employees, new Comparator<Employee>() {
public int compare(Employee emp1, Employee emp2) {
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
}
});
assertEquals(employees, employeesSortedByDateDesc);
}
We can also convert the above method to more concise forms using the Java 8 Lambda Expressions. This will perform the same functions as the above function, with the only difference that the code contains fewer lines of code in comparison to the above code. Though this also makes the code less readable. While using Comparator, we pass an anonymous inner class on-the-fly for the Collections.sort() API:
我们也可以使用Java 8 Lambda Expressions将上述方法转换成更简洁的形式。这将执行与上述函数相同的功能,唯一不同的是,与上述代码相比,代码包含的行数更少。尽管这也使得代码的可读性降低。在使用比较器时,我们为Collections.sort() API实时传递一个匿名的内部类。
@Test
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {
Collections.sort(employees, (emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
assertEquals(employees, employeesSortedByDateDesc);
}
5. Conclusion
5.总结
In this article, we explored how to sort a Java Collection by Date object in both ascending and descending modes.
在这篇文章中,我们探讨了如何通过Date对象以升序和降序模式对Java Collection进行排序。
We also briefly saw the Java 8 lambda features that are useful in sorting and help in making the code concise.
我们还简要地看到了Java 8的lambda功能,这些功能在排序中很有用,有助于使代码简洁。
As always, the complete code examples used in this article can be found over on GitHub.
一如既往,本文中使用的完整代码示例可以在GitHub上找到。