In this tutorial, we’ll show you some ways and functions to sort list of Objects in Dart/Flutter. At the end, you’re gonna know how to:
- sort list of objects by property (ascending/descending)
- sort list of objects by date
- sort list of objects by two fields
Related Posts:
– Dart/Flutter List Tutorial with Examples
– Dart/Flutter Constructors tutorial with examples
– Dart/Flutter String Methods & Operators tutorial with examples
– Dart/Flutter Future Tutorial with Examples
– Dart/Flutter Map Tutorial with Examples
– Dart/Flutter – Convert Object to JSON string
– Dart/Flutter – Convert/Parse JSON string, array into Object, List
– Dart/Flutter – Convert List to Map & Map to List
– Dart/Flutter – Sort Map by Key/Value
Contents
Dart/Flutter Sort a List of objects by property
I will show you two ways:
– using custom compare function.
– extending Comparable
abstract class and override compareTo()
method
Approach 1: using custom compare function
We’re gonna pass a custom compare function into list’s sort()
method.
First we sort the list of objects by field name
, then we sort by field age
, separately.
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) => a.age.compareTo(b.age));
print('Sort by Age: ' + customers.toString());
customers.sort((a, b) => a.name.compareTo(b.name));
print('Sort by Name: ' + customers.toString());
}
Output:
Sort by Age: [{ Jack, 23 }, { Katherin, 25 }, { Adam, 27 }]
Sort by Name: [{ Adam, 27 }, { Jack, 23 }, { Katherin, 25 }]
Approach 2: using Comparable
abstract class
The second approach is to extend Comparable
abstract class and override compareTo()
method. Now we don’t need to pass compare
function, we just call list.sort()
instead of list.sort(compare)
.
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.sort();
print(customers);
}
Output:
[{ Adam, 27 }, { Jack, 23 }, { Katherin, 25 }]
Dart/Flutter Sort a List of objects Descending
Similar to Ascending order, but you can do this with three ways:
– using custom compare function.
– extending Comparable
abstract class, override compareTo()
method and using reversed
.
– extending Comparable
abstract class, override compareTo()
method and using fast_immutable_collections library.
Approach 1: 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 }]
Approach 2: 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.
Approach 3: 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 }]
Dart/Flutter Sort list of objects by date
I will show you two ways:
– using custom date compare function.
– extending Comparable
abstract class and override compareTo()
method with date comparision,
Approach 1: using custom compare function
We’re gonna pass a custom compare function into list’s sort()
method.
First we sort the list of objects by field name
, then we sort by field age
, separately.
class Customer {
String name;
int age;
DateTime createdAt;
Customer(this.name, this.age, this.createdAt);
@override
String toString() {
return '{ ${this.name}, ${this.createdAt} }';
}
}
main() {
List<Customer> customers = [];
customers.add(Customer('Jack', 23, DateTime.utc(2022, 12, 9)));
customers.add(Customer('Adam', 27, DateTime.utc(2022, 5, 18)));
customers.add(Customer('Katherin', 25, DateTime.utc(2023, 8, 1)));
customers.sort((a, b) => a.createdAt.compareTo(b.createdAt));
print('Sort by Date: ' + customers.toString());
}
Output:
Sort by Date: [{ Adam, 2022-05-18 00:00:00.000Z }, { Jack, 2022-12-09 00:00:00.000Z }, { Katherin, 2023-08-01 00:00:00.000Z }]
Approach 2: using Comparable
abstract class
class Customer extends Comparable {
String name;
int age;
DateTime createdAt;
Customer(this.name, this.age, this.createdAt);
@override
String toString() {
return '{ ${this.name}, ${this.createdAt} }';
}
// sort by Date
@override
int compareTo(other) {
return this.createdAt.compareTo(other.createdAt);
}
}
main() {
List<Customer> customers = [];
customers.add(Customer('Jack', 23, DateTime.utc(2022, 12, 9)));
customers.add(Customer('Adam', 27, DateTime.utc(2022, 5, 18)));
customers.add(Customer('Katherin', 25, DateTime.utc(2023, 8, 1)));
customers.sort();
print('Sort by Date: ' + customers.toString());
}
Output:
Sort by Date: [{ Adam, 2022-05-18 00:00:00.000Z }, { Jack, 2022-12-09 00:00:00.000Z }, { Katherin, 2023-08-01 00:00:00.000Z }]
Dart/Flutter Sort list of objects by two fields
For sorting multiple fields, or two fields for this tutorial. I will also show you two ways:
– using custom compare function.
– extending Comparable
abstract class and override compareTo()
method
Approach 1: using custom compare function
We’re gonna pass a more complicated custom compare function into list’s sort()
method.
First we sort the list of objects by field name
, then field age
.
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:
Sort by Name(ASC), then Age(DESC):
[{ Adam, 27 }, { Jack, 32 }, { Jack, 23 }, { Katherin, 25 }]
Approach 2: using Comparable
abstract class
The second approach is to extend Comparable
abstract class and override compareTo()
method. Now we don’t need to pass compare
function, we just call list.sort()
instead of list.sort(compare)
.
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:
Sort by Name(ASC), then Age(DESC):
[{ Adam, 27 }, { Jack, 32 }, { Jack, 23 }, { Katherin, 25 }]
Conclusion
In this tutorial, we’ve learned some ways and functions to sort list of Objects in Dart/Flutter. Finally, you’ve known how to sort list of objects by property (ascending/descending), by date, or by two fields.
For sorting a Map, kindly visit:
Dart/Flutter – Sort Map by Key/Value
Happy learning! See you again!
Further Reading
– Dart/Flutter List Tutorial with Examples
– Dart/Flutter Constructors tutorial with examples
– Dart/Flutter String Methods & Operators tutorial with examples
– Dart/Flutter Future Tutorial with Examples
– Dart/Flutter Map Tutorial with Examples