Java sort List by date as String

前端 未结 2 1223
不知归路
不知归路 2021-01-26 05:50

I have a list from the Type and I would like to sort this list by an element of date. I googled and I saw some solutions with comparable, but is there a possibility to do this

2条回答
  •  逝去的感伤
    2021-01-26 06:36

    So if date is stored in varchar2 than firstly Convert them to an actual Date object,

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd h:m");
    System.out.println(sdf.parse(dateFromDataBase));
    

    and then do the comparing on date Objects

    if(date1.compareTo(date2)>0){
    // Do whatever you want to do 
    }
    

    Now coming to the sorting of List

    you can use comparator Interface as mentioned by @Srinivasu Talluri but if you are not interested in using these interfaces which literally make you life easy than you can use either bubble sort or Insertion sort and compare on the basis of dates as mentioned above

提交回复
热议问题