Dart Sort List of objects Descending
Tags: dart, todayilearned, programming, flutter
Published: 2022-03-26 08:10:05

In this tutorial, we'll show you some ways and functions to sort list of Objects descending in Dart/Flutter

Similar to Ascending order, but you can do this with three ways:
- using custom compare function.
- extending <code>Comparable</code> abstract class, override <code>compareTo()</code> method and using <code>reversed</code>.
- extending <code>Comparable</code> abstract class, override <code>compareTo()</code> method and using <a href="https://pub.dev/packages/fast_immutable_collections">fast_immutable_collections</a> library.

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

<h2>Dart/Flutter Sort List of objects Descending using custom compare function</h2>
Pass a custom compare function into list's <code>sort()</code> method with swap the places of the items.

```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) => b.age.compareTo(a.age));
  print('Sort by Age: ' + customers.toString());

  customers.sort((a, b) => b.name.compareTo(a.name));
  print('Sort by Name: ' + customers.toString());
}
```

Output:
<pre><code>Sort by Age: [{ Adam, 27 }, { Katherin, 25 }, { Jack, 23 }]
Sort by Name: [{ Katherin, 25 }, { Jack, 23 }, { Adam, 27 }]
</code></pre>

<h2>Dart/Flutter Sort List of objects Descending using Comparable abstract class</h2>
We can sort a List of objects in Descending order using <a href="https://api.dart.dev/stable/dart-core/List/reversed.html">reversed</a> property. It returns an <code>Iterable</code> of the objects in the list.

```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.reversed.toList();
  print(customers);
}
```
Output:
<pre><code>[{ Katherin, 25 }, { Jack, 23 }, { Adam, 27 }]
</code></pre>

Firstly, we extend <code>Comparable</code> abstract class and override <code>compareTo()</code> method, then we call <code>list.reversed.toList()</code>.

<code>.reversed</code> does NOT reverse the list. Instead, it sorts the original list in <strong>Ascending</strong> order, then returns a <strong>Descending</strong> <code>Iterable</code>. SO we need <code>.toList()</code> finally.

However, how about large lists?
Let's look at another approach.

<h2>Dart/Flutter Sort List of objects Descending using a library</h2>
We can create our immutable collections and use <em>Fast Immutable Collections</em> library directly.
The class is the same as previous approach (extends <code>Comparable</code> and override <code>compareTo()</code> method). But it uses <code>sortReversed()</code> which is much faster.

```dart
import 'package:fast_immutable_collections/fast_immutable_collections.dart';

class Customer extends Comparable {
  ...
  @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.sortReversed();
  print(customers);
}
```
Output:
<pre><code>[{ Katherin, 25 }, { Jack, 23 }, { Adam, 27 }]
</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>