经常遇见一个列表,两个接口的情况,两个接口属于两个不同的表数据,那么数据拼接回来之后,并不是按照时间排序的,看起来就相当混乱,所以记录一下如何对数据按照时间排序。
步骤一:
格式化日期
1
2
3
4
5
6
|
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; } |
步骤二:
对拼接的列表进行排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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,比较数据大小时,这里比的是时间 */ @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); } |
直接调用这个方法,数据类型改造一下即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_16131393/article/details/75289079