如何按日期字段对集合进行排序?

发布于 2024-12-01 05:58:57 字数 862 浏览 1 评论 0原文

我正在尝试按发行日期对专辑进行排序。由于某种原因,我一无所获:

//sort by release date
Collections.sort(this._items, new Comparator<Album>() {
    public int compare(Album t1, Album t2) {
        int dateCmp = t2.getStartDate().compareTo(t1.getStartDate());
        Log.i("==Albums==", "dateComp: " + dateCmp);
        return t1.getStartDate().compareTo(t2.getStartDate());
    }            
});

我做错了什么?

以下是我在 Foo Fighters 专辑的输出中看到的按发行日期排列的内容:

name: There Is Nothing Left To Lose | release date: 11/2/1999
name: Greatest Hits | release date: 11/3/2009
name: Skin And Bones | release date: 11/7/2006
name: Foo Fighters | release date: 12/10/2003
name: DOA | release date: 12/13/2005
name: Rope | release date: 3/1/2011
name: The Colour And The Shape | release date: 3/30/2010

I'm trying to sort an album by release date. For some reason I am not getting anywhere:

//sort by release date
Collections.sort(this._items, new Comparator<Album>() {
    public int compare(Album t1, Album t2) {
        int dateCmp = t2.getStartDate().compareTo(t1.getStartDate());
        Log.i("==Albums==", "dateComp: " + dateCmp);
        return t1.getStartDate().compareTo(t2.getStartDate());
    }            
});

What am I doing wrong?

Here is what I see in the output of Foo Fighters Albums by release date:

name: There Is Nothing Left To Lose | release date: 11/2/1999
name: Greatest Hits | release date: 11/3/2009
name: Skin And Bones | release date: 11/7/2006
name: Foo Fighters | release date: 12/10/2003
name: DOA | release date: 12/13/2005
name: Rope | release date: 3/1/2011
name: The Colour And The Shape | release date: 3/30/2010

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦初启 2024-12-08 05:58:57

看起来您的 startDate 字段的类型为 java.lang.String。至少,输出中的顺序证实了这一点。 String#compareTo() 将按字典顺序对 String 值进行排序,而不是按其在观看者眼中所代表的值进行排序。

如果将错误的类型更改为 java.lang. util.Date,或使用 SimpleDateFormat#parse()compare() 中将 String 转换为 Date 方法,然后调用 Date#compareTo() 相反,排序将按预期进行。

我将其替换为 java.util.Date。始终对其表示的值使用正确的类型。

It look like that your startDate field is of type java.lang.String. At least, the order in the output confirms that. The String#compareTo() will order String values lexicographically, not by the value it represents in the eye of the beholder.

If you change the incorrect type to be java.util.Date, or use SimpleDateFormat#parse() to convert the String to Date inside the compare() method and then call Date#compareTo() instead, then the ordering will work as expected.

I'd replace it by java.util.Date. Always use the right type for the value it represents.

国产ˉ祖宗 2024-12-08 05:58:57

这是完整的代码。问题是因为开始日期是字符串类型。

//按照发布日期排序

Collections.sort(this._items, new Comparator<Album>() {    
                        public int compare(Album t1, Album t2) {

                            int dateCmp = 0;

                             try {  

                                SimpleDateFormat formatter; 
                                Date date1; 
                                Date date2; 
                                formatter = new SimpleDateFormat("dd/mm/yyyy");

                                date1 = (Date)formatter.parse(t1.getStartDate()); 
                                date2 = (Date)formatter.parse(t2.getStartDate()); 

                                dateCmp = (date2).compareTo(date1);

                              } 
                              catch (Exception e)
                              {
                                //Log.i("==Albums==", "album special sort - error" );
                              }

                            //Log.i("==Albums==", "dateComp: " + dateCmp);

                            return dateCmp; 

                        }               
                    });

Here is the full code. The issue was because the start date was of type string.

//sort by release date

Collections.sort(this._items, new Comparator<Album>() {    
                        public int compare(Album t1, Album t2) {

                            int dateCmp = 0;

                             try {  

                                SimpleDateFormat formatter; 
                                Date date1; 
                                Date date2; 
                                formatter = new SimpleDateFormat("dd/mm/yyyy");

                                date1 = (Date)formatter.parse(t1.getStartDate()); 
                                date2 = (Date)formatter.parse(t2.getStartDate()); 

                                dateCmp = (date2).compareTo(date1);

                              } 
                              catch (Exception e)
                              {
                                //Log.i("==Albums==", "album special sort - error" );
                              }

                            //Log.i("==Albums==", "dateComp: " + dateCmp);

                            return dateCmp; 

                        }               
                    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文