【问题标题】:Dart - How to order datetime objects?Dart - 如何订购日期时间对象?
【发布时间】:2018-10-24 11:09:00
【问题描述】:

使用 Flutter/Dart(新手)。我需要将用户结果存储在游戏中(分数、时间和名称)。我认为 GameResult 类是要走的路吗?

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

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    Dart DateTime 类是 Comparable 到其他 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)";
     }
    

    这将创建一个可以相互比较实例的类。 顺序是先倒序score(先高分再低分),对于相同的分数,它会将较早的结果排在后面的结果之前。

    Comparable.compareTo 方法指定如果接收者在顺序中“在”参数“之前”返回负数,如果它们具有相同的顺序,则返回零,如果接收者在参数之后,则返回正数。

    整数也具有可比性,通过使用 other.score.compareTo(score) 而不是相反,我们有效地切换了分数的顺序 - 整数默认为较低的值,然后是较高的值)。

    Result 类与其自身具有可比性时,您可以将List&lt;Result&gt; 排序为:

    var results = <Result>[
       Result("Me", 0, DateTime.now().subtract(Duration(hours: 1))),
       Result("Me again", 0, DateTime.now().subtract(Duration(hours: 2))),
       Result("You", 1000000, DateTime.now().subtract(Duration(hours: 24))), 
    ];
    results.sort();
    print(results.join("\n"));
    

    打印出来:

    Result(You, score: 1000000, time: 2018-10-23 15:34:16.532)
    Result(Me again, score: 0, time: 2018-10-24 13:34:16.532)
    Result(Me, score: 0, time: 2018-10-24 14:34:16.531)
    

    你以分数获胜。我之前的零分在我后来的零分之前。

    【讨论】:

    • 完美运行。谢谢你提供了一个完整的例子,我几乎可以剪切并粘贴到我的应用程序中。我现在不能投票给你,当我有足够的特权时我会的。
    【解决方案2】:

    一种方法是将日期评估为 Epoch DateTime millisecondsSinceEpoch,然后按标准 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);
    

    【讨论】:

      猜你喜欢
      • 2018-09-29
      • 2020-06-21
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2021-05-30
      • 2018-08-29
      相关资源
      最近更新 更多