dart - Dart按两个属性排序列表

标签 dart

我有一个带有相同对象的列表。

我想通过两个属性对它们全部进行排序

名(从a到Z)

第二种类型(数字递增,例如1,2,3 ...)

void main() {
  List<MyObject> list = List();
  list.add(MyObject('Apple', 'definition of type #1'));
  list.add(MyObject('Strawberry', 'definition of type #8'));
  list.add(MyObject('Banana', 'definition of type #2'));
  list.add(MyObject('Orange', 'definition of type #3'));
  list.add(MyObject('Kiwi', 'definition of type #1'));
  list.add(MyObject('Peach', 'definition of type #4'));
  list.add(MyObject('Orange', 'definition of type #1'));
  list.add(MyObject('Apple', 'definition of type #8'));
  list.add(MyObject('Peach', 'definition of type #2'));
  list.add(MyObject('Strawberry', 'definition of type #17'));
  list.add(MyObject('Peach', 'definition of type #1'));
  list.add(MyObject('Banana', 'definition of type #5'));
  list.add(MyObject('Apple', 'definition of type #16'));
  list.add(MyObject('Strawberry', 'definition of type #7'));

  for (MyObject _object in list) {
    print(_object.name + ' ' + _object.type);
  }

  RegExp regExp = new RegExp(
    r"#'?.*",
  );

  list.sort((a, b) {
    int _a = int.parse(regExp.stringMatch(a.type).replaceAll('#', ''));
    int _b = int.parse(regExp.stringMatch(b.type).replaceAll('#', ''));

    return _a
        .compareTo(_b); //to get the order other way just switch `adate & bdate`
  });

  list.sort((a, b) => a.name.compareTo(b.name));
}

class MyObject {
  String name;
  String type;

  MyObject(this.name, this.type);
}

我想要这样的输出,
Apple #1
Apple #8
Apple #16
Banana #2
Banana #5
...

我试图使用正则表达式方法解析数字(来自“类型为#(number)的定义”)

但是,如果我以此对列表进行排序,则无法将列表从a到Z(名称)进行排序

最佳答案

您只需要排序一次并定义排序方法,以便在名称相同的情况下比较类型:

void main() {
  List<MyObject> list = List();
  list.add(MyObject('Apple', 'definition of type #1'));
  list.add(MyObject('Strawberry', 'definition of type #8'));
  list.add(MyObject('Banana', 'definition of type #2'));
  list.add(MyObject('Orange', 'definition of type #3'));
  list.add(MyObject('Kiwi', 'definition of type #1'));
  list.add(MyObject('Peach', 'definition of type #4'));
  list.add(MyObject('Orange', 'definition of type #1'));
  list.add(MyObject('Apple', 'definition of type #8'));
  list.add(MyObject('Peach', 'definition of type #2'));
  list.add(MyObject('Strawberry', 'definition of type #17'));
  list.add(MyObject('Peach', 'definition of type #1'));
  list.add(MyObject('Banana', 'definition of type #5'));
  list.add(MyObject('Apple', 'definition of type #16'));
  list.add(MyObject('Strawberry', 'definition of type #7'));

  RegExp regExp = new RegExp(
    r"#'?.*",
  );

  list.sort((a, b) {
    int compare = a.name.compareTo(b.name);

    if (compare == 0) {
      int _a = int.parse(regExp.stringMatch(a.type).replaceAll('#', ''));
      int _b = int.parse(regExp.stringMatch(b.type).replaceAll('#', ''));

      return _a.compareTo(_b);
    } else {
      return compare;
    }
  });

  for (MyObject _object in list) {
    print(_object.name + ' ' + _object.type);
  }
}

class MyObject {
  String name;
  String type;

  MyObject(this.name, this.type);
}

输出:

Apple definition of type #1
Apple definition of type #8
Apple definition of type #16
Banana definition of type #2
Banana definition of type #5
Kiwi definition of type #1
Orange definition of type #1
Orange definition of type #3
Peach definition of type #1
Peach definition of type #2
Peach definition of type #4
Strawberry definition of type #7
Strawberry definition of type #8
Strawberry definition of type #17

关于dart - Dart按两个属性排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61343000/

相关文章:

flutter - Flutter Future构建在加载时未显示容器

javascript - 从 dart 创建 javascript 对象

flutter - 如何在 Dart 中取消延迟的 future ?

flutter - 如何通过Flutter应用在Instagram feed上发布链接?

flutter - 文字输入值消失

dart - 为什么 FocusNode 需要在 flutter 中配置?

dart - 我们可以在 flutter 中使用 fcm 发送图像推送通知吗?

flutter - 使用 DraggableScrollableSheet 时 BoxConstraints 强制无限高度

flutter - backpress 将用户带到列表中的第一个图像

class - 如何从Flutter中的另一个类访问一个类的变量