sort array list of special strings, by date

前端 未结 6 602
有刺的猬
有刺的猬 2021-01-25 10:33

I have an arrayList.

This is an arrayList of strings. The string contains a \"Date.toString\" in format of \"January 1, 1970, 00:00:00 GMT\" +

6条回答
  •  遥遥无期
    2021-01-25 10:49

    I would convert this array list first to something like

    public class Event {
        private Date date;
        private String description;
        ...
    }
    

    After that, create a comparator to compare 2 events like this:

    public class EventComparator implements Comparator{
        public int compare(Event e1, Event e2) {
            return e1.getDate().compareTo(e2.getDate());
        }
    }
    

    This will save you tons of performance compared with (pardon me) parsing each string for each comparison.

提交回复
热议问题