英文:
how to sort arraylist of java records
问题
我有一个类似这样的记录定义:
public record UserCourseSubscriptionRec(
Integer userId,
String userFirstName,
String userLastName,
Integer courseId,
String courseName,
Date startDate,
Date endDate) {
}
我有一个包含这些记录的数组列表。
如何按userId升序然后按startDate降序对这个数组列表进行排序?
我知道如何对类进行排序,但是我不能将这个记录更改为类。
英文:
I have a record definition like this:
public record UserCourseSubscriptionRec(
Integer userId,
String userFirstName,
String userLastName,
Integer courseId,
String courseName,
Date startDate,
Date endDate) {
}
I have an arrayList of there records.
How can I sort this arrayList by userId ascending then startDate descending?
I know how to do it for a class, but I cannot change this record to a class.
答案1
得分: 1
首先,你应该使用LocalDate
而不是Date
,因为后者已经过时、不建议使用,并且存在缺陷。LocalDate
和其他类可以在java.time包中找到,并提供广泛的功能。
public record UserCourseSubscriptionRec(
Integer userId,
String userFirstName,
String userLastName,
Integer courseId,
String courseName,
Date startDate,
Date endDate) {
}
定义一个比较器。
Comparator<UserCourseSubscriptionRec> comp = Comparator
.comparing(UserCourseSubscriptionRec::userId)
.thenComparing(UserCourseSubscriptionRec::startDate,
Comparator.reverseOrder());
然后使用ArrayList的排序方法与比较器。
YourList.sort(comp);
或者如f1sh在评论中建议的,你可以让你的类或记录实现Comparable
接口。下面是可能的实现方式。
record UserCourseSubscriptionRec(Integer userId, String userFirstName,
String userLastName, Integer courseId, String courseName,
Date startDate, Date endDate)
implements Comparable<UserCourseSubscriptionRec> { // 实现接口
public int compareTo(UserCourseSubscriptionRec ucs) {
// 首先,按升序比较用户ID
int result = userId.compareTo(ucs.userId);
// 如果结果非零,则返回结果。
// 否则,返回按照相反顺序排序的结果
// (改变`compareTo`的参数顺序将颠倒自然顺序)。
return result != 0 ? result :
ucs.startDate.compareTo(startDate);
}
}
然后只需按如下方式调用排序。null
参数表示使用Comparable
接口指定的排序顺序。
YourList.sort(null);
英文:
First, you should be using LocalDate
and not Date
as the latter is obsolete, deprecated and buggy. That and other classes are available in the java.time package and offer a wide range of capabilities.
public record UserCourseSubscriptionRec(
Integer userId,
String userFirstName,
String userLastName,
Integer courseId,
String courseName,
Date startDate,
Date endDate) {
}
Define a comparator.
Comparator<UserCourseSubscriptionRec> comp = Comparator
.comparing(UserCourseSubscriptionRec::userId)
.thenComparing(UserCourseSubscriptionRec::startDate,
Comparator.reverseOrder());
Then use the ArrayList sort method with the comparator.
YourList.sort(comp);
Or as f1sh suggested in the comments, you can have your class or record implement the Comparable
Here interface. Here is how that might look.
record UserCourseSubscriptionRec(Integer userId, String userFirstName,
String userLastName, Integer courseId, String courseName,
Date startDate, Date endDate)
implements Comparable<UserCourseSubscriptionRec> { // implement the interface
public int compareTo(UserCourseSubscriptionRec ucs) {
// first, compare the userid's in ascending order
int result = userId.compareTo(ucs.userId);
// if non-zero, return the result.
// otherwise, return the result of sorting in reverse order
// (changing the `compareTo` argument order reverses the natural order).
return result != 0 ? result :
ucs.startDate.compareTo(startDate);
}
}
Then just call sort as follows. The null
argument says use the ordering specified by the Comparable
complementation.
YourList.sort(null);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论