Tengo una lista con los mismos objetos.

Y me gustaría dos ordenarlos todos por dos propiedades

Nombre (de la A a la Z)

Segundo tipo (números ascendentes, como 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);
}

Quiero una salida como esta,

Apple #1
Apple #8
Apple #16
Banana #2
Banana #5
...

Traté de usar un método regex para analizar números (de 'definición de tipo # (número)')

Pero si clasifico la lista por esto, no podría ordenar la lista de la A a la Z (nombre)

1
developer.alp 21 abr. 2020 a las 14:57

2 respuestas

La mejor respuesta

Solo necesita ordenar una vez y definir el método de clasificación para que compare el tipo si el nombre es el mismo:

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

Salida:

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
2
julemand101 21 abr. 2020 a las 12:43

Prefiero implementar una interfaz comparable para la clase MyObject, por lo que la clasificación será bastante fácil

Ver ejemplo completo

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

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(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';
}

Se imprime

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
3
Genchi Genbutsu 21 abr. 2020 a las 12:45