开发者

Sort Arraylists according to Dates with particular format

I have a problem that I want to sort multipl开发者_如何转开发e Arraylists with respect to Date Arraylist with a format as: 2011-07-18T10:39:32.144Z. How to that in java? Can anyone Help me out?

Thanks in advance.


First of all, the format of the date is irrelevant, as long as they are represented by the Date class.

Since Date implements Comparable, the standard sort of the Collections class should work.

Considering "dates" as your ArrayList, just do: Collections.sort(dates);


use this code

Collections.sort(arrayListOfDates, new Comparator(){
                @Override
                public int compare(Object o1, Object o2) {
                    Date date1=(Date)o1;
                    Date date2=(Date)o1;
                    if(date1.equals(date2))
                        return 0;
                    else if(date1.before(date2))
                        return 1;
                    else
                        return -1;
                }
            });

It sorts the ArrayList Of dates and stores it in the same list

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜