如何根据dateTIme和TimeOfDay对flutter/dart中的列表项进行排序?



我正在制作一个Todo应用程序,所以我想根据用户选择的日期和时间对任务列表进行排序,目前我是这么说的,但这并不总是准确的,还有其他建议吗??

class TaskModel { //model of task
final String? id;
final String? title;
final DateTime? date;
final String? field;
final TimeOfDay? time;
bool isFinished;
TaskModel({
@required this.date,
@required this.id,
@required this.title,
@required this.time,
this.field = 'other',
this.isFinished = false,
});
//method right now I'm using to compare(not efficient)
_tasks.sort((a, b) => a.date!.compareTo(b.date!));
_tasks.sort((a, b) => a.time!.hour.compareTo(b.time!.hour));
_tasks.sort((a, b) => a.time!.minute.compareTo(b.time!.minute));

use this:

YOURLIST.sort((a, b)=> a.microsecondsSinceEpoch.compareTo(b.microsecondsSinceEpoch));

您可以从用户选择的日期和时间解析DateTime,如下所示:

DateTime testDate = DateTime.parse("2021-08-01 20:18:04");
print(testDate.millisecondsSinceEpoch);

所以你可以用selectedDate selectedTime代替2021-08-01 20:18:04

相关内容

  • 没有找到相关文章