2017-05-29 39 views
-1

我有一个时间戳列表(长),我必须根据时间或列表添加排序列表。如何对时间戳列表进行排序?

我已经尝试过,但其现在的工作。

Collections.sort(vehicles, new Comparator<VehicleListModel.Vehicle>() { 
       @Override 
       public int compare(VehicleListModel.Vehicle o1, VehicleListModel.Vehicle o2) { 
        try { 
         DateFormat format = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss"); 
         return format.parse(o1.getCreated()).compareTo(format.parse(o2.getCreated())); 
        } catch (Exception e) { 
         e.printStackTrace(); 
         return 0; 
        } 
       } 
      }); 
      customerListAdapter.notifyDataSetChanged(); 

它不工作,然后我试图 this但这Date(timeStamp)已被弃用

请帮助

+2

***我已经尝试过,但现在正在努力工作***这不足以帮助我们! **如何**不起作用? –

+0

这是什么字符串:getCreated()? –

+0

getCreated()将返回时间戳添加到服务现在我必须根据时间戳进行排序 –

回答

1

如果getCreated返回Date那么你可以使用compareTo和排序基于这种比较列表,如:

List<VehicleListModel.Vehicle> list = new ArrayList<>(); 
list.sort((e1, e2) -> e1.getCreated().compareTo(e2.getCreated())); 

更新

如果getCreated回报long值,那么你可以框,并使用LongcompareTo方法,如:

List<ChainCode> list = new ArrayList<ChainCode>(); 
list.sort((e1, e2) -> new Long(e1.getCreated()).compareTo(new Long(e2.getCreated()))); 
+0

先生,\t getCreated()行返回时间戳加入到服务现在我必须根据排序时间戳 –

+0

@KRoy是指'java.sql.Timestamp'还是只是一个'long'值? –

+0

超值! @Darshan –

0

使用长时间戳值soring

 Collections.sort(vehicles, new Comparator<VehicleListModel.Vehicle>() { 
        @Override 
        public int compare(VehicleListModel.Vehicle o1, VehicleListModel.Vehicle o2) { 
         try { 
          DateFormat format = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss"); 
return Long.valueOf(format.parse(o1.getCreated()).getTime()).compareTo(format.parse(o2.getCreated()).getTime()); 

         } catch (Exception e) { 
          e.printStackTrace(); 
          return 0; 
         } 
        } 
       }); 
       customerListAdapter.notifyDataSetChanged(); 
+0

.compareTo()表示无法解析方法 –

+0

日期可比,所以这应该不会有太大的改变。 –

+0

getCreated()行返回时间戳加入到服务现在我有基于时间戳排序长 –

相关问题