Android中對list的日期元素進行排序

來源:互聯網
上載者:User

標籤:

最近在項目中需要將讀取的資料按照時間的降序進行排序。

具體的步驟如下:

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;  

Android中對list的日期元素進行排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.