【问题标题】:Java Date sort using compartor使用比较器的 Java 日期排序
【发布时间】:2017-09-28 23:46:51
【问题描述】:

我正在尝试对日期列表进行排序,但它不起作用。

这是AttemptEntity中的声明和get函数

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "end_time")
private Date endTime;

public Date getEndTime() {
    return endTime;
}

这是没有做任何事情的排序代码。 GetAttempts() 获取所有被调用尝试的列表。它们不按顺序排列,我只想能够获得具有最新 endTime 的任何尝试。

        List<AttemptEntity> attempts = called.getAttempts();
        Collections.sort(attempts, new Comparator<AttemptEntity>() {
        @Override
        public int compare(AttemptEntity a1, AttemptEntity a2) {
            if (a1.getEndTime() == null || a2.getEndTime() == null)
                return 0;
            return a1.getEndTime().compareTo(a2.getEndTime());
            }
        });

我相信上面的代码应该对尝试进行排序,然后在尝试之后进行排序,所以最后的结束时间应该是尝试.get(attempts.size()-1).getEndTime()

我尝试使用以下内容,但也根本没有进行任何排序。输入列表和输出列表完全相同。

Comparator<AttemptEntity> comparator = Comparator.comparing(AttemptEntity::getEndTime).reversed();
attempts.sort(comparator);

【问题讨论】:

  • 如果有没有endTime 的尝试,您的compare 方法会认为它等于每个 其他实例。我不确定这样的通配符会对二进制排序过程产生什么影响。

标签: java sorting date collections comparator


【解决方案1】:

您的代码对我来说一切都很好,所以尝试一下您的代码,我可以对日期进行排序,如果您执行的操作与以下不同,请告诉我:

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

public class DateSorter {
    static List<AttemptEntity> attempts = null;
    public static void main(String[] args) {
        prepareTestData();
        System.out.println(attempts);
        sortDates();
        System.out.println(attempts);
    }

    private static void sortDates() {
        Collections.sort(attempts, new Comparator<AttemptEntity>() {
            @Override
            public int compare(AttemptEntity a1, AttemptEntity a2) {
                if (a1.getEndTime() == null){
                    return 0;
                } else if(a2.getEndTime() == null){
                    return -1;
                }
                return a1.getEndTime().compareTo(a2.getEndTime());
            }
        });
    }

    private static void prepareTestData() {
        attempts = new ArrayList<>();
        attempts.add(new AttemptEntity(new Date(4444)));
        attempts.add(new AttemptEntity(new Date(1111)));
        attempts.add(new AttemptEntity(null));
        attempts.add(new AttemptEntity(new Date(3333)));
        attempts.add(new AttemptEntity(new Date(2222)));
    }
}

测试 POJO:

import java.util.Date;

public class AttemptEntity {

    private Date endTime;

    AttemptEntity(Date date){
        endTime = date;
    }

    public Date getEndTime() {
        return endTime;
    }

    @Override
    public String toString() {
        return endTime == null ? null : endTime.toString();
    }

}

最终输出:

[Thu Jan 01 05:30:04 IST 1970, Thu Jan 01 05:30:01 IST 1970, null, Thu Jan 01 05:30:03 IST 1970, Thu Jan 01 05:30:02 IST 1970]
[Thu Jan 01 05:30:01 IST 1970, Thu Jan 01 05:30:02 IST 1970, Thu Jan 01 05:30:03 IST 1970, Thu Jan 01 05:30:04 IST 1970, null]

更新:看起来一些 NULL 日期可能会导致一些排序问题,我已经更新了上面代码中的 compare 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多