如何对Java记录的ArrayList进行排序。

huangapple go评论57阅读模式
英文:

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&lt;UserCourseSubscriptionRec&gt; 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&lt;UserCourseSubscriptionRec&gt; { // implement the interface

    public int compareTo(UserCourseSubscriptionRec ucs) {
        // first, compare the userid&#39;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>



huangapple
  • 本文由 发表于 2023年2月24日 00:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定