Sort objects in ArrayList by date?

后端 未结 14 2436
生来不讨喜
生来不讨喜 2020-11-22 06:58

Every example I find is about doing this alphabetically, while I need my elements sorted by date.

My ArrayList contains objects on which one of the datamembers is a

14条回答
  •  不要未来只要你来
    2020-11-22 07:20

    All the answers here I found to be un-neccesarily complex for a simple problem (at least to an experienced java developer, which I am not). I had a similar problem and chanced upon this (and other) solutions, and though they provided a pointer, for a beginner I found as stated above. My solution, depends on where in the the Object your Date is, in this case, the date is the first element of the Object[] where dataVector is the ArrayList containing your Objects.

    Collections.sort(dataVector, new Comparator() {
        public int compare(Object[] o1, Object[] o2) {
            return ((Date)o1[0]).compareTo(((Date)o2[0]));
        }
    });
    

提交回复
热议问题