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
Comparable
abstract class, overridecompareTo()
method and usingreversed
. - extending
Comparable
abstract class, overridecompareTo()
method and using fast_immutable_collections library.
Full post: Dart/Flutter Sort List of objects
Dart/Flutter Sort List of objects Descending using custom compare function
Pass a custom compare function into list's sort()
method with swap the places of the items.
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:
Sort by Age: [{ Adam, 27 }, { Katherin, 25 }, { Jack, 23 }]
Sort by Name: [{ Katherin, 25 }, { Jack, 23 }, { Adam, 27 }]
Dart/Flutter Sort List of objects Descending using Comparable abstract class
We can sort a List of objects in Descending order using reversed property. It returns an Iterable
of the objects in the list.
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:
[{ Katherin, 25 }, { Jack, 23 }, { Adam, 27 }]
Firstly, we extend Comparable
abstract class and override compareTo()
method, then we call list.reversed.toList()
.
.reversed
does NOT reverse the list. Instead, it sorts the original list in Ascending order, then returns a Descending Iterable
. SO we need .toList()
finally.
However, how about large lists?
Let's look at another approach.
Dart/Flutter Sort List of objects Descending using a library
We can create our immutable collections and use Fast Immutable Collections library directly.
The class is the same as previous approach (extends Comparable
and override compareTo()
method). But it uses sortReversed()
which is much faster.
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:
[{ Katherin, 25 }, { Jack, 23 }, { Adam, 27 }]