【问题标题】:Sort two lists the same way以相同的方式对两个列表进行排序
【发布时间】:2021-02-26 17:18:24
【问题描述】:

我需要从最早到最晚对 DateTime 列表进行排序。

List<DateTime> list = [2021-01-15 12:26:40.709246, 2021-02-25 13:26:40.709246, 2021-02-20 19:26:40.709246];
datetimeList.sort();

我还有另一个字符串列表。

List<String> list = ["one", "two", "three"];

stringList 的索引必须与 datetimeList 的索引匹配。所以“一”的索引必须始终与 2021-01-15 12:26:40.709246 的索引相同。

如果我单独对列表进行排序,则 DateTime 按 DateTime 排序,而 Strings 按字母顺序排序。这样一来,字符串就不再使用它的初始日期了。

如何对一个列表 (datetimeList) 进行排序,而另一个列表 (stringList) 的排序方式完全相同?

【问题讨论】:

    标签: sorting dart


    【解决方案1】:

    最简单的解决方案是创建一个结构/类来组合这两个变量,这样您就不必担心保持数组中的对象对齐。您需要做的最后一件事是按日期对数组 ob 新对象进行排序。为此,由于缺少有关 Dart 的知识,我无法为您提供帮助。

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    您也可以使用 SplayTreeMap。https://api.dart.dev/stable/2.8.4/dart-collection/SplayTreeMap-class.html

    SplayTreeMap 确保其键按排序顺序排列。您可以将日期时间用作键,将其他列表的内容用作值。

    
    main() {
      final SplayTreeMap<DateTime, String> map =
          new SplayTreeMap<DateTime, String>();
      map[DateTime.parse("2021-01-15 12:26:40.709246")] = "one";
      map[DateTime.parse("2021-02-25 13:26:40.709246")] = "three";
      map[DateTime.parse("2021-02-20 19:26:40.709246")] = "two";
    
      for (final DateTime key in map.keys) {
        print("$key : ${map[key]}");
      }
    }
    

    【讨论】:

      【解决方案3】:

      我推荐这里给出的更简单的建议。

      为了完整起见,我将提供另一种方法:通过对索引列表进行排序来计算排列:

      List<int> sortedPermutation<T>(List<T> elements, int compare(T a, T b)) =>
        [for (var i = 0; i < elements.length; i++) i]
            ..sort((i, j) => compare(elements[i], elements[j]));
      

      然后您可以重新排序现有列表以匹配:

      List<T> reorder<T>(List<T> elements, List<int> permutation) =>
        [for (var i = 0; i < permutation.length; i++) elements[permutation[i]]];
      

      如果你这样做:

      var sorted = reorder(original, sortedPermutation(original, compare));
      

      它应该给你一个排序列表。

      它比就地排序效率低,因为您创建了一个新列表, 但您可以在之后对多个列表应用相同的重新排序。

      【讨论】:

        【解决方案4】:

        快速且非常有效的方法。

        void main() {
          final l1 = [3, 1, 2];
          final l2 = ['three', 'one', 'two'];
          final l3 = ['drei', 'ein', 'zwei'];
        
          print(l1);
          print(l2);
          print(l3);
        
          myCompare(int x, int y) => x.compareTo(y);
        
          l1.sortLists([l2, l3], myCompare);
        
          print('============');
        
          print(l1);
          print(l2);
          print(l3);
        }
        
        extension SortListByList<E> on List<E> {
          sortLists(Iterable<List> lists, int Function(E, E) compare) {
            for (final list in lists) {
              if (list.length != length) {
                throw StateError('The length of lists must be equal');
              }
            }
        
            final rules = <int>[];
            sort((x, y) {
              final rule = compare(x, y);
              rules.add(rule);
              return rule;
            });
        
            for (final list in lists) {
              var rule = 0;
              list.sort((x, y) => rules[rule++]);
            }
          }
        }
        

        输出:

        [3, 1, 2]
        [three, one, two]
        [drei, ein, zwei]
        ============
        [1, 2, 3]
        [one, two, three]
        [ein, zwei, drei]
        

        【讨论】:

          猜你喜欢
          • 2012-10-20
          • 1970-01-01
          • 2017-03-06
          • 2012-04-12
          • 2016-01-02
          • 2012-04-03
          相关资源
          最近更新 更多