【问题标题】:Sorting Lexicographically in Dart在 Dart 中按字典顺序排序
【发布时间】:2019-01-07 20:48:35
【问题描述】:

Firebase 数据库按字典顺序对结果进行排序,我发现无法在 Dart 中保留这种排序。

如何在 Dart 中按字典顺序排序,或保留 Firebase 数据库提供的顺序?

【问题讨论】:

  • 由于排序发生在服务端,并且 Flutter 中的代码使用相同的底层 SDK,因此 Flutter 中的行为应该与其他 SDK 中的行为相同。如果您不是这种情况,请编辑您的问题以包含 minimal code that reproduces the problem
  • 在 firebase 上创建一堆条目,进行 onValue 读取,与在 JS 客户端中执行相同操作相比,它们的顺序不同。将它们放在一个列表中并使用 a.compareTo(b) 进行排序,它们不会以相同的顺序排列。也就是说,问题不是关于 Firebase,而是关于在 Dart 中按字典顺序排序

标签: firebase-realtime-database dart flutter


【解决方案1】:

看这里:Sort List by alphabetical order

  List<String> input = ["b", "a", "c", "aa"];
  print(input.toString());

  input.sort((a, b) {
    return a.toLowerCase().compareTo(b.toLowerCase());
  });
  print(input.toString());  // [a, aa, b, c]

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 2020-08-02
    • 2015-06-23
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    相关资源
    最近更新 更多