Dart 按两个属性排序列表

dev*_*alp 1 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);
}
Run Code Online (Sandbox Code Playgroud)

我想要这样的输出,

Apple #1
Apple #8
Apple #16
Banana #2
Banana #5
...
Run Code Online (Sandbox Code Playgroud)

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

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

小智 6

要进行完整的比较,您应该实现Comparable< MyObject >,它公开了compareTo()一种按一个或多个属性排序的方法。

请参阅下面的完整示例:

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'));

  list.sort();
  for (MyObject _object in list) {
    print(_object);
  }
}

abstract class Comparable {
   int compareTo(Object obj){
   }
}

class MyObject implements Comparable<MyObject> {
  static final RegExp _regExp = RegExp(r'\D+');

  final String name;
  final String type;

  MyObject(this.name, this.type);

  @override
  int compareTo(MyObject other) {
    int result = name?.compareTo(other?.name) ?? 0;
    if (result == 0) {
      int type1 = int.tryParse(type?.replaceAll(_regExp, '') ?? 0);
      int type2 = int.tryParse(other?.type?.replaceAll(_regExp, '') ?? 0);
      result = type1.compareTo(type2);
    }
    return result;
  }

  @override
  String toString() => '$name $type';
}
Run Code Online (Sandbox Code Playgroud)

它打印

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
Run Code Online (Sandbox Code Playgroud)


jul*_*101 5

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

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);
}
Run Code Online (Sandbox Code Playgroud)

输出:

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);
}
Run Code Online (Sandbox Code Playgroud)