Sorting List of Objects/Maps By Date In Flutter
This article will explore the Sorting List of Objects/Maps By Date In Flutter. We will perceive how to execute a demo program and we are going to learn about how we can use it in your applications.
If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.
Table Of Contents::
Sorting a List of Objects by Date
Sorting a List of Maps by Date
Introduction:
The point is to utilize the sort() technique for the List class and pass a custom correlation capability that utilizes the compareTo() strategy for the DateTime class:
myList.sort ((a, b) {
// for ascending order
return a.date.compareTo (b.date);
// for descending order
// return b.date.compareTo (a.date);
});
Sorting a List of Objects by Date:
Suppose you have a list of users. The data of every user is stored away in an object, and your supervisor requests that you reorder these users by their join date. The model underneath will show you to tackle this:
The code:
class User {
final String name;
final String email;
final DateTime joinDate;
User({required this.name, required this.email, required this.joinDate});
@override
String toString() {
return 'User{name: $name, email: $email, joinDate: $joinDate}';
}
}
void main() {
// list of users
List<User> users = [
User(
name: 'Rahul',
email: 'raul@gmail.com',
joinDate: DateTime(2022, 10, 3)),
User(
name: 'John',
email: 'john@gmail.com',
joinDate: DateTime(2019, 5, 19)),
User(
name: 'Sam',
email: 'sam@gmail.com',
joinDate: DateTime(2021, 5, 12)),
User(
name: 'RSorted users by ascending joinDate:
[{name: John, email: john@gmail.com, joinDate: 2019-05-19}, {name: Sam, email: sam@gmail.com, joinDate: 2021-05-12}, {name: Rahul, email: raul@gmail.com, joinDate: 2022-10-03}, {name: Ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08}]
Sorted users by descending joinDate:
[{name: Ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08}, {name: Rahul, email: raul@gmail.com, joinDate: 2022-10-03}, {name: Sam, email: sam@gmail.com, joinDate: 2021-05-12}, {name: John, email: john@gmail.com, joinDate: 2019-05-19}]
Process finished with exit code 0uchi',
email: 'ruchi@gmail.com',
joinDate: DateTime(2023, 2, 8))
];
// sort by joinDate: ascending
final sortedUsersAsc = users.map((user) => user).toList()
..sort((a, b) => a.joinDate.compareTo(b.joinDate));
// sort by joinDate: descending
final sortedUsersDesc = users.map((user) => user).toList()
..sort((a, b) => b.joinDate.compareTo(a.joinDate));
print('Sorted by joinDate: ascending');
sortedUsersAsc.forEach((user) => print(user.toString()));
print('Sorted by joinDate: descending');
sortedUsersDesc.forEach((user) => print(user.toString()));
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
Sorted by joinDate: ascending
User{name: John, email: john@gmail.com, joinDate: 2019-05-19 00:00:00.000}
User{name: Sam, email: sam@gmail.com, joinDate: 2021-05-12 00:00:00.000}
User{name: Rahul, email: raul@gmail.com, joinDate: 2022-10-03 00:00:00.000}
User{name: Ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08 00:00:00.000}
Sorted by joinDate: descending
User{name: ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08 00:00:00.000}
User{name: rahul, email: raul@gmail.com, joinDate: 2022-10-03 00:00:00.000}
User{name: Sam, email: sam@gmail.com, joinDate: 2021-05-12 00:00:00.000}
User{name: John, email: john@gmail.com, joinDate: 2019-05-19 00:00:00.000}
Process finished with exit code 0
Sorting a List of Maps by Date:
In this model, the data about a user is stored away on a map rather than an object. Other than that, the join date of a user is a string, not a DateTime object. Hence, we should parse it into a DateTime object first utilizing the DateTime.parse() or DateTime.tryParse() strategy before playing out the comparison.
The code:
void main() {
final List<Map<String, dynamic>> users = [
{
"name": "Rahul",
"email": "raul@gmail.com",
"joinDate": "2022-10-03",
},
{
"name": "John",
"email": "john@gmail.com",
"joinDate": "2019-05-19",
},
{
"name": "Sam",
"email": "sam@gmail.com",
"joinDate": "2021-05-12",
},
{
"name": "Ruchi",
"email": "ruchi@gmail.com",
"joinDate": "2023-02-08",
}
];
// sort users by joinDate: ascending
final sortedUsersAsc = users.map((user) => user).toList()
..sort((a, b) =>
DateTime.parse(a["joinDate"]).compareTo(DateTime.parse(b["joinDate"])));
// sort users by joinDate: descending
final sortedUsersDesc = users.map((user) => user).toList()
..sort((a, b) =>
DateTime.parse(b["joinDate"]).compareTo(DateTime.parse(a["joinDate"])));
print("Sorted users by ascending joinDate:");
print(sortedUsersAsc);
print("Sorted users by descending joinDate:");
print(sortedUsersDesc);
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
Sorted users by ascending joinDate:
[
{name: John, email: john@gmail.com, joinDate: 2019-05-19},
{name: Sam, email: sam@gmail.com, joinDate: 2021-05-12},
{name: Rahul, email: raul@gmail.com, joinDate: 2022-10-03},
{name: Ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08}
]
Sorted users by descending joinDate:
[
{name: Ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08},
{name: Rahul, email: raul@gmail.com, joinDate: 2022-10-03},
{name: Sam, email: sam@gmail.com, joinDate: 2021-05-12},
{name: John, email: john@gmail.com, joinDate: 2019-05-19}
]
Process finished with exit code 0
Conclusion:
In the article, I have explained the sorting list of Objects/Maps By Date In Flutter; you can modify this code according to your choice. This was a small introduction to Sorting a List of Objects/Maps By Date In Flutter User Interaction from my side, and it’s working using Flutter.
I hope this blog will provide you with sufficient information on how to try the Sorting List of Objects/Maps by the date of your flutter projects. So please try it.
❤ ❤ Thanks for reading this article ❤❤
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
Feel free to connect with us:
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on Facebook, GitHub, Twitter, and LinkedIn.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.