最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android实现数据按照时间排序
时间:2022-06-25 22:47:49 编辑:袖梨 来源:一聚教程网
经常遇见一个列表,两个接口的情况,两个接口属于两个不同的表数据,那么数据拼接回来之后,并不是按照时间排序的,看起来就相当混乱,所以记录一下如何对数据按照时间排序。
步骤一:
格式化日期
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(ArrayListmList) { Collections.sort(mList, new Comparator () { /** * * @param lhs * @param rhs * @return an integer 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); }
直接调用这个方法,数据类型改造一下即可。
相关文章
- 用 AI 完成需求后,我发现真正的难点在代码验收 09-11
- openbazaar-go:实践指南 09-11
- DataWorks Data Agent 实战课(八):搭建数据质量巡检服务 09-11
- lgtm:实践指南 09-11
- DoRA:实践指南 09-11
- Codex 进阶实践:用可验收规格定义编码任务 09-11