Android實現(xiàn)數(shù)據(jù)按照時間排序
更新時間:2018年09月24日 09:45:16 作者:隔壁小王66
這篇文章主要為大家詳細介紹了Android實現(xiàn)數(shù)據(jù)按照時間排序的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
經(jīng)常遇見一個列表,兩個接口的情況,兩個接口屬于兩個不同的表數(shù)據(jù),那么數(shù)據(jù)拼接回來之后,并不是按照時間排序的,看起來就相當混亂,所以記錄一下如何對數(shù)據(jù)按照時間排序。
步驟一:
格式化日期
public static Date stringToDate(String dateString) {
ParsePosition position = new ParsePosition(0);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateValue = simpleDateFormat.parse(dateString, position);
return dateValue;
}
步驟二:
對拼接的列表進行排序
private void sortData(ArrayList<CourseModel> mList) {
Collections.sort(mList, new Comparator<CourseModel>() {
/**
*
* @param lhs
* @param rhs
* @return an integer < 0 if lhs is less than rhs, 0 if they are
* equal, and > 0 if lhs is greater than rhs,比較數(shù)據(jù)大小時,這里比的是時間
*/
@Override
public int compare(CourseModel lhs, CourseModel rhs) {
Date date1 = DateUtil.stringToDate(lhs.getCREATE_TIME());
Date date2 = DateUtil.stringToDate(rhs.getCREATE_TIME());
// 對日期字段進行升序,如果欲降序可采用after方法
if (date1.before(date2)) {
return 1;
}
return -1;
}
});
adapter.replaceAll(mList);
}
直接調(diào)用這個方法,數(shù)據(jù)類型改造一下即可。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Arduino 數(shù)據(jù)類型轉換(單機片)詳細介紹
這篇文章主要介紹了Arduino 數(shù)據(jù)類型轉換(單機片)詳細介紹的相關資料,需要的朋友可以參考下2016-11-11
Android實現(xiàn)可拖拽的GridView效果長按可拖拽刪除數(shù)據(jù)源
這篇文章主要介紹了Android實現(xiàn)可拖拽的GridView效果長按可拖拽刪除數(shù)據(jù)源,要實現(xiàn)的基本功能是長按,移到垃圾桶,刪除數(shù)據(jù),需要的朋友可以參考下2017-12-12

