Dart sort List by two fields
Tags: dart, flutter, todayilearned, beginners
Published: 2022-03-26 08:04:51

In this tutorial, we'll show you some ways and functions to sort list by two fields in Dart/Flutter

For sorting multiple fields, or two fields for this tutorial. I will also show you two ways:
- using custom compare function.
- extending <code>Comparable</code> abstract class and override <code>compareTo()</code> method

Full post: [Dart/Flutter Sort List of objects](https://www.bezkoder.com/dart-sort-list-of-objects/)

<h2>Dart Sort list by two fields using custom compare function</h2>
We're gonna pass a more complicated custom compare function into list's <code>sort()</code> method.
First we sort the list of objects by field <code>name</code>, then field <code>age</code>.

```dart
class Customer {
  String name;
  int age;

  Customer(this.name, this.age);

  @override
  String toString() {
    return '{ ${this.name}, ${this.age} }';
  }
}

main() {
  List<Customer> customers = [];
  customers.add(Customer('Jack', 23));
  customers.add(Customer('Adam', 27));
  customers.add(Customer('Katherin', 25));

  customers.sort((a, b) {
    int nameComp = a.name.compareTo(b.name);
    if (nameComp == 0) {
      return -a.age.compareTo(b.age); // '-' for descending
    }
    return nameComp;
  });
  print('Sort by Name(ASC), then Age(DESC):\n' + customers.toString());
}
```

Output:
<pre><code>Sort by Name(ASC), then Age(DESC):
[{ Adam, 27 }, { Jack, 32 }, { Jack, 23 }, { Katherin, 25 }]
</code></pre>

<h2>Dart Sort list by two fields using Comparable abstract class</h2>
The second approach is to extend <code>Comparable</code> abstract class and override <code>compareTo()</code> method. Now we don't need to pass <code>compare</code> function, we just call <code>list.sort()</code> instead of <code>list.sort(compare)</code>.

```dart
class Customer extends Comparable {
  String name;
  int age;

  Customer(this.name, this.age);

  @override
  String toString() {
    return '{ ${this.name}, ${this.age} }';
  }

  // sort by Name (asc)
  @override
  int compareTo(other) {
    return this.name.compareTo(other.name);
  }
}

main() {
 List<Customer> customers = [];
  customers.add(Customer('Jack', 23));
  customers.add(Customer('Adam', 27));
  customers.add(Customer('Katherin', 25));
  customers.add(Customer('Jack', 32));

  customers.sort();
  print('Sort by Name(ASC), then Age(DESC):\n' + customers.toString());
}
```
Output:
<pre><code>Sort by Name(ASC), then Age(DESC):
[{ Adam, 27 }, { Jack, 32 }, { Jack, 23 }, { Katherin, 25 }]
</code></pre>

## Further Reading
- <a href="https://www.bezkoder.com/dart-list/">Dart/Flutter List Tutorial with Examples</a>
- <a href="https://www.bezkoder.com/dart-flutter-constructors/">Dart/Flutter Constructors tutorial with examples</a>
- <a href="https://www.bezkoder.com/dart-string-methods-operators-examples/">Dart/Flutter String Methods & Operators tutorial with examples</a>
- <a href="https://www.bezkoder.com/dart-future/">Dart/Flutter Future Tutorial with Examples</a>
- <a href="https://www.bezkoder.com/dart-map/">Dart/Flutter Map Tutorial with Examples</a>

- <a href="https://www.bezkoder.com/dart-flutter-convert-object-to-json-string/">Dart/Flutter – Convert Object to JSON string</a>
- <a href="https://www.bezkoder.com/dart-flutter-parse-json-string-array-to-object-list/">Dart/Flutter – Convert/Parse JSON string, array into Object, List</a>
- <a href="https://www.bezkoder.com/dart-convert-list-map/">Dart/Flutter – Convert List to Map & Map to List</a>