【发布时间】:2021-09-23 19:06:42
【问题描述】:
仓库在下面
https://github.com/kanari3/flutter_sort_sample
我按startedDate对项目进行排序,然后尝试在startedDate范围内按startedDatetime排序。
class Item {
//required
int id;
DateTime createdAt;
DateTime startedDate;
DateTime endedDate;
//optional
DateTime startedDatetime;
DateTime endedDatetime;
在编写 if 表达式时,我注意到 return 0 是乱序的。
How to use the dart sort method (with sample code)
第一个排序结果
result
id, startedDate, startedDateTime, endedDatetime
2, 2020-11-06 00:00:00.000, null , null
1, 2020-11-06 00:00:00.000, 2020-11-06 08:00:00.000, 2020-11-06 17:00:00.000
3, 2020-11-06 00:00:00.000, null , null
4, 2020-11-07 00:00:00.000, 2020-11-07 08:00:00.000, 2020-11-07 17:00:00.000
5, 2020-11-07 00:00:00.000, null , null
6, 2020-11-07 00:00:00.000, null , null
7, 2020-11-08 00:00:00.000, 2020-11-08 08:00:00.000, 2020-11-08 17:00:00.000
在这里,startedDate 已正确排序,但仍不是理想的结果。
我的理解。 我认为 sort 方法如果 item1 和 item2 大于等于 1 则交换 item1 和 item2,如果小于 0 则不执行任何操作。
但结果如下(第二次排序结果)
result
id, startedDate, startedDateTime, endedDatetime
12, 2020-11-12 00:00:00.000, null , null
1, 2020-11-06 00:00:00.000, 2020-11-06 08:00:00.000, 2020-11-06 17:00:00.000
3, 2020-11-06 00:00:00.000, null , null
.
.
为什么会这样? 另外,我该如何解决这个问题?
测试如下
https://github.com/kanari3/flutter_sort_sample/blob/master/test/sort_test.dart
【问题讨论】: