【问题标题】:Sorting of Arraylist by date only sorts day and not month and year?按日期对 Arraylist 进行排序仅对日期进行排序,而不是对月份和年份进行排序?
【发布时间】:2012-05-27 03:08:49
【问题描述】:

我正在尝试按(类型日期)MyObject.getDate() 对 ArrayList 进行排序

我的格式是由 dd-mm-yyyy 的 SimpleDateFormat 格式化的; 我正在使用自定义比较器对列表进行排序。 我正在使用 Collections.sort(productList, new CustomComparator())

问题我的排序列表仅按 dd 而不是 dd-mm-yyyy。

有什么想法吗?

public class Product
{
    private String productName; 
    private Date boughtOn;
    private Date useBy; 
    private double boughtAt; 
    private long quantity; 
    private static DateFormat dateFormat;


    public Date getUseBy()
    {
        return useBy;
    }
    public void setUseBy(Date useBy)
    {
        this.useBy = useBy;
    }
}

public static Date rtnDate(String dateStr, int currentLineCount)
    {
        SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy");
        try
        {   
            Date date = df.parse(dateStr);
            return date;
        }
        catch (ParseException e)
        {
            productParsingErrorMessages.add("Date parse exception: " + e + "on line " + currentLineCount);
            return null;
        }

//      DateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
//      Date date2 = (Date)formatter.parse(dateStr);
    }
System.out.println("Product list - BEFORE sorting by UseBy");
printInventory(produuctList);

Collections.sort(produuctList, new CustomComparator());

System.out.println("Product list - AFTER sorting by UseBy");
printInventory(produuctList);
public class CustomComparator implements Comparator<Product>
{
    @Override
    public int compare(Product o1, Product o2)
    {
        return o1.getUseBy().compareTo(o2.getUseBy());
    }
}

产品列表 - 按 UseBy 排序之前

产品名称:糖使用时间:2012 年 12 月 12 日
产品名称: 糖 UseBy : 01-10-2012
产品名称: 糖 使用时间: 16-04-2012
产品名称: 糖 使用时间: 30-01-2012
产品名称:糖使用时间:25-04-2012
产品名称: 糖 使用时间: 03-04-2012
产品名称:糖UseBy : 08-04-2012

产品列表 - 按 UseBy 排序后

产品名称:糖使用时间:01-10-2012
产品名称: 糖 使用时间: 03-04-2012
产品名称: 糖 UseBy : 08-04-2012
产品名称: 糖 使用时间: 12-12-2012
产品名称: 糖 使用时间: 16-04-2012
产品名称:糖使用时间:25-04-2012
产品名称:糖UseBy : 30-01-2012

【问题讨论】:

  • 请发布您的代码,否则我们会猜测发生了什么。
  • 似乎是 compareTo(date1, date2) 方法的问题。
  • 你能提供 compareTo() 的实现吗
  • 发布代码,而不是图片。
  • 来吧。我们是志愿者,所以应该有责任让我们轻松地为您提供帮助。我们要求您发布有问题的代码,而不是图片,也不是代码链接。

标签: java sorting arraylist date-sorting


【解决方案1】:

如果您的格式字符串已正确复制,则您的 SimpleDateFormat 错误。应该是dd-MM-yyyy,而不是dd-mm-yyyy,因为您感兴趣的是,而不是分钟

你有:

public static Date rtnDate(String dateStr, int currentLineCount)
{
    SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy");

什么时候应该使用:

public static Date rtnDate(String dateStr, int currentLineCount)
{
    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");

这可以解释一切。

【讨论】:

    【解决方案2】:

    我大约 90% 确定您在比较 java.lang.Strings 而不是 java.util.Dates。鉴于您提到的日期格式,它显然会首先将所有日期排序在一起。例如。一整年,您的清单将开始:

    01-01-2012 (1 Jan)
    01-02-2012 (1 Feb)
    01-03-2012 (1 Mar)
    

    使用 Date 对象进行比较,而不是它的 String 表示。

    附:图片不是代码。将您的代码放入您的问题中。

    更新:证明您发布的代码按预期工作:

    import java.text.DateFormat;
    import java.util.*;
    
    public class DateComparators {
        public static void main(String[] args) {
            List<Product> productList = Arrays.asList(
                    new Product(new Date(2012 - 1900, 5, 1)), // 1 Jun 2012
                    new Product(new Date(2012 - 1900, 1, 1)), // 1 Feb 2012
                    new Product(new Date(2012 - 1900, 2, 15)), // 15 Mar 2012
                    new Product(new Date(2012 - 1900, 0, 2)), // 2 Jan 2012
                    new Product(new Date(2012 - 1900, 1, 2)), // 2 Feb 2012
                    new Product(new Date(2012 - 1900, 0, 1)) // 1 Jan 2012
            );
            Collections.sort(productList, new CustomComparator());
            System.out.println(productList);
        }
    
        static class CustomComparator implements Comparator<Product> {
            @Override
            public int compare(Product o1, Product o2) {
                return o1.getUseBy().compareTo(o2.getUseBy());
            }
        }
    
        static class Product {
            private String productName; // sauce pan
            private Date boughtOn; // 4-5-2012
            private Date useBy; // 4-5-2012
            private double boughtAt; // $4.05
            private long quantity; // 9
            private static DateFormat dateFormat;
    
            Product(Date useBy) {
                this.useBy = useBy;
            }
    
            public Date getUseBy() {
                return useBy;
            }
    
            public void setUseBy(Date useBy) {
                this.useBy = useBy;
            }
    
            @Override
            public String toString() {
                return "\nuseBy=" + useBy;
            }
        }
    }
    

    输出:

    [
    useBy=Sun Jan 01 00:00:00 CST 2012, 
    useBy=Mon Jan 02 00:00:00 CST 2012, 
    useBy=Wed Feb 01 00:00:00 CST 2012, 
    useBy=Thu Feb 02 00:00:00 CST 2012, 
    useBy=Thu Mar 15 00:00:00 CDT 2012, 
    useBy=Fri Jun 01 00:00:00 CDT 2012]
    

    因此,您还没有向我们展示一切。问题出在你遗漏的地方。

    【讨论】:

    • @DavidB:所以他声称,但我坚持我的猜测。如我的更新所示,他显示的代码对日期进行了正确排序。我们没有看到产生上述效果的代码。
    • 不,实际上你没有。您已经展示了一些独立的代码,但没有任何东西可以创建您所描述的输出。在开始排序之前,您需要定义一个 main 方法和一个产品列表。
    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2021-11-03
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多