Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Sort list by two properties

Tags:

dart

I have a List with same objects.

And I would like two sort all of them by two properties

First name (from a to Z)

Second type (numbers by ascending, like 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);
}

I want output like this,

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

I tried to use a regex method for parsing numbers (from 'definition of type #(number)')

But If I sort list by this, I could not sort list from a to Z (name's)

like image 563
alperen.emeksiz Avatar asked Oct 26 '25 02:10

alperen.emeksiz


1 Answers

You just need to sort once and define the sorting method so it compares the type if name are the same:

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

Output:

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
like image 121
julemand101 Avatar answered Oct 29 '25 06:10

julemand101