Dart-如何对日期时间对象进行排序?

Dart-如何对日期时间对象进行排序?,dart,flutter,Dart,Flutter,使用颤振/飞镖(新手)。我需要在游戏中存储用户结果(分数、时间和名称)。我想GameResult类是最好的选择 然后,我需要按日期/时间(从最新到最旧)对每个用户的结果进行排序,以显示结果表。如何对日期时间值进行排序?Dart类是针对其他datetime对象的,因此您可以使用dateTime1.compareTo(dateTime2)来比较两个时间点 例如: class Result implements Comparable<Result> { final String na

使用颤振/飞镖(新手)。我需要在游戏中存储用户结果(分数、时间和名称)。我想GameResult类是最好的选择

然后,我需要按日期/时间(从最新到最旧)对每个用户的结果进行排序,以显示结果表。如何对日期时间值进行排序?

Dart类是针对其他
datetime
对象的,因此您可以使用
dateTime1.compareTo(dateTime2)
来比较两个时间点

例如:

class Result implements Comparable<Result> {
   final String name;
   final int score;       
   final DateTime time;
   Result(this.name, this.score, this.time);

   int compareTo(Result other) {
     int order = other.score.compareTo(score);
     if (order == 0) order = time.compareTo(other.time);
     return order;
   }

   String toString() => "Result($name, score: $score, time: $time)";
 }
这张照片是:

结果(你,分数:1000000,时间:2018-10-23 15:34:16.532)
结果(又是我,得分0,时间:2018-10-2413:34:16.532)
结果(Me,得分:0,时间:2018-10-2414:34:16.531)

你以分数取胜。我之前的零分在我的后一个零分之前。

一种方法是将日期评估为历元日期时间
毫秒历元
,然后作为标准int值排序

使用这种方法,您可以在一行中对整个列表进行排序

result.sort((a,b) => a.date.millisecondsSinceEpoch.compareTo(b.date.millisecondsSinceEpoch));
下面是一个使用游戏结果场景的完整示例

// Create the game result object
class Result{

    Result(this.name, this.score, this.date);
    String name;
    int score;
    DateTime date;

    @override    
    String toString() => '$name  $score  $date';
}


// Create a list to put the results in
List<Result> result = [];


// Populate the list with results
result..add( Result('Tom',  850, DateTime.tryParse('2020-01-10')))
      ..add( Result('Dic',   600, DateTime.tryParse('2020-01-03')))
      ..add( Result('Harry', 450, DateTime.tryParse('2020-01-05')));

// print the unsorted list
print(result);

// Sort the list by datetime property
result.sort((a,b) => a.date.millisecondsSinceEpoch.compareTo(b.date.millisecondsSinceEpoch));


// print the sorted list
print(result);
//创建游戏结果对象
班级成绩{
结果(this.name、this.score、this.date);
字符串名;
智力得分;
日期时间日期;
@凌驾
字符串toString()=>“$name$score$date”;
}
//创建一个列表,将结果放入其中
列表结果=[];
//用结果填充列表
结果..添加(结果('Tom',850,DateTime.tryParse('2020-01-10'))
…添加(结果('Dic',600,DateTime.tryParse('2020-01-03'))
…添加(结果('Harry',450,DateTime.tryParse('2020-01-05'));
//打印未排序的列表
打印(结果);
//按datetime属性对列表进行排序
result.sort((a,b)=>a.date.millissecondssinceepoch.compareTo(b.date.millissecondssinceepoch));
//打印已排序的列表
打印(结果);

工作正常。谢谢你提供了一个完整的例子,我几乎可以剪切并粘贴到我的应用程序中。我现在不能投你一票,等我得到足够的特权我就会投你一票。
// Create the game result object
class Result{

    Result(this.name, this.score, this.date);
    String name;
    int score;
    DateTime date;

    @override    
    String toString() => '$name  $score  $date';
}


// Create a list to put the results in
List<Result> result = [];


// Populate the list with results
result..add( Result('Tom',  850, DateTime.tryParse('2020-01-10')))
      ..add( Result('Dic',   600, DateTime.tryParse('2020-01-03')))
      ..add( Result('Harry', 450, DateTime.tryParse('2020-01-05')));

// print the unsorted list
print(result);

// Sort the list by datetime property
result.sort((a,b) => a.date.millisecondsSinceEpoch.compareTo(b.date.millisecondsSinceEpoch));


// print the sorted list
print(result);