簡體   English   中英

在Java中按日期字段排序對象列表

[英]Sorting list of objects by date field in Java

我一直在嘗試很多(非常很多)代碼,以使我的對象列表按日期字段排序,嘗試使用比較器,lambda和long等。

我什么都沒做,我的清單只是沒有排序,我真的不知道該怎么辦,我在這里搜索了很多遍,並嘗試了所有發現的東西。

我有一個帖子列表,有一個名為created的java.util.Date字段,這些帖子與名為category的實體具有多對多關系。

首先,我創建了一個類別:

Category cat = catCtrl.get(5);

然后我得到了帖子列表:

List<Post> list = cat.getPostList();

打印出列表:(ID +創建的+標題)

2017年11月25日(星期六)03:36:06 MST多尼奇(libec lacinia)
2017年11月27日星期一02:46:37 MST 2017 Etiam at bibendum mauris
47 Sat Nov 25 03:39:17 MST 2017 Cras vel orci nunc
2017年11月25日星期六星期六03:37:29 MST dasdasf

現在,想法是按創建日期從最近到舊的順序排序列表:

2017年11月27日星期一02:46:37 MST 2017 Etiam at bibendum mauris
47 Sat Nov 25 03:39:17 MST 2017 Cras vel orci nunc
2017年11月25日星期六星期六03:37:29 MST dasdasf
2017年11月25日(星期六)03:36:06 MST多尼奇(libec lacinia)

根據官方文檔,應該這樣做:

Collections.sort(list, new Comparator<Post>() {
    public int compare(Post p1, Post p2) {
        return p1.getCreated().compareTo(p2.getCreated());
    }
});

但是,該代碼根本不會改變順序。

我一直想念什么?

@Poxxac

使用下面的代碼,為我工作。

package com.java;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;

public class Test {

    public static void main(String[] args) {

        DateFormat df = new SimpleDateFormat("yyyyMMdd");

        Example example1 = new Example();
        example1.setDate(new Date());
        example1.setName("Name1");

        Example example2 = new Example();
        example2.setDate(new Date(System.currentTimeMillis()-48*60*60*1000));
        example2.setName("Name2");

        Example example3 = new Example();
        example3.setDate(new Date(System.currentTimeMillis()-24*60*60*1000));
        example3.setName("Name3");

        Multimap<String, Example> multimap = MultimapBuilder.treeKeys().linkedListValues().build();
        multimap.put(df.format(example1.getDate()), example1);
        multimap.put(df.format(example2.getDate()), example2);
        multimap.put(df.format(example3.getDate()), example3);


        System.out.println(multimap);

    }
}

class Example {

    private String name;

    private Date date;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "Example [name=" + name + ", date=" + date + "]";
    }

}

測試功能:

public void test()
{
    Date date1 = DateUtils.convertToDate("2017-11-20");
    Date date2 = DateUtils.convertToDate("2017-11-21");
    Date date3 = DateUtils.convertToDate("2017-11-22");

    Demo demo1 = new Demo(1, date1);
    Demo demo2 = new Demo(2, date2);
    Demo demo3 = new Demo(3, date3);
    List<Demo> demos = new ArrayList<Demo>();
    demos.add(demo1);
    demos.add(demo2);
    demos.add(demo3);

    for(Demo item : demos){
        System.out.println(item.toString());
    }

    Collections.sort(demos, new Comparator<Demo>(){
        public int compare(Demo o1, Demo o2){
            return o2.create.compareTo(o1.create);
        }
    });
    System.out.println("========== ordered =============");
    for(Demo item : demos){
        System.out.println(item.toString());
    }
}

並輸出:

1-Mon Nov 20 00:00:00 GMT+08:00 2017
2-Tue Nov 21 00:00:00 GMT+08:00 2017
3-Wed Nov 22 00:00:00 GMT+08:00 2017
========== ordered =============
3-Wed Nov 22 00:00:00 GMT+08:00 2017
2-Tue Nov 21 00:00:00 GMT+08:00 2017
1-Mon Nov 20 00:00:00 GMT+08:00 2017

還有一個問題,您可能正在使用類java.util.LinkedList講述這些datas

注意 :類DateUtils是我自己的定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM