Java sort List by date as String

前端 未结 2 1224
不知归路
不知归路 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-26 06:54

    Collections.sort(Type, new Comparator() {
        @Override
        public int compare(Type o1, Type o2) {
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd h:m");
              Date d1 = sdf.parse(o1.date);
              Date d2 = sdf.parse(o2.date);
              return d1.compareTo(d2);
          }
      });
    

提交回复
热议问题