Reputation: 25
How to sort a List by one of its value; I want to sort it by the 'name' alphabetically.
List<dynamic> list = [
{
'name':'abc',
'other imformations':'-',
},
{
'name':'abc',
'other imformations':'-',
}
]
I have tried (copied from another post):
list.sort((a, b) {
var x = a['name'].compareTo(b['name']);
if (x != 0)
return x;
return a['name'].compareTo(b['name']);
});
I have also tried:
list.sort(
(a, b) => a['name'].toString().toLowerCase().compareTo(
b['name'].toString().toLowerCase(),
),
);
Upvotes: 2
Views: 3130
Reputation: 120
If you want to print list that is sorted by name, then can use this code below:
print(list..sort((a, b) => a['name'].compareTo(b['name'])));
Upvotes: 5