zoukankan      html  css  js  c++  java
  • Android中对list的日期元素进行排序

    最近在项目中需要将读取的数据按照时间的降序进行排序。

    具体的步骤如下:

    1.读取数据,存入List中

    2.取出数据中的时间戳,由String转换成Date

    3.使用冒泡排序对List中元素按照Date进行排序

    具体代码如下:

    //将List按照时间倒序排列

    @SuppressLint("SimpleDateFormat") 

    private List<TestEntity> invertOrderList(List<TestEntity> L){  

          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

          Date d1;  

          Date d2;  

          TestEntity temp_r = new TestEntity();

        //做一个冒泡排序,大的在数组的前列 

    for(int i=0; i<L.size()-1; i++){

        for(int j=i+1; j<L.size();j++){  

                 ParsePosition pos1 = new ParsePosition(0);  

                 ParsePosition pos2 = new ParsePosition(0);  

                 d1 = sdf.parse(L.get(i).getDate(), pos1);  

                 d2 = sdf.parse(L.get(j).getDate(), pos2); 

          if(d1.before(d2)){//如果队前日期靠前,调换顺序  

                  temp_r = L.get(i);  

                     L.set(i, L.get(j));  

                     L.set(j, temp_r);  

               }  

            }  

        }  

    return L;  

  • 相关阅读:
    BUG记录
    .Net HTTP请求的发送方式与分析
    初始token
    VS2017开发安卓应用(Xamarin)
    路由模板和路由特性
    使用signalR创建聊天室。
    C# SessionHelper
    postgres递归查询所有子部门
    centos7备份postgres
    Centos7挂载硬盘
  • 原文地址:https://www.cnblogs.com/Jingerxin/p/5938901.html
Copyright © 2011-2022 走看看