In this tutorial, I will show you way to Sort Map in Dart/Flutter. You will know how to:
- Sort Map by Key
- Sort Map by Value
- Sort Map by Key using SplayTreeMap
- Sort Map by Value using SplayTreeMap
Related Posts:
– Dart/Flutter List Tutorial with Examples
– Dart/Flutter Map Tutorial with Examples
– Dart/Flutter Future Tutorial with Examples
– Dart/Flutter – Sort list of Objects
– Dart/Flutter Constructors tutorial with examples
– Dart/Flutter String Methods & Operators tutorial with examples
Contents
Dart/Flutter Sort Map by Key
We can sort a Map by Key with following steps:
- get all Entries of the Map using
entries
property - convert the Entries into a List with toList() method
- sort the List using sort() method and compare function (for entries’ keys)
- convert the sorted List into Map with fromEntries() method
Map map = {3: 'three', 1: 'one', 4: 'four', 5: 'five', 2: 'two'};
var sortedByKeyMap = Map.fromEntries(
map.entries.toList()..sort((e1, e2) => e1.key.compareTo(e2.key)));
print(sortedByKeyMap);
Output:
{1: one, 2: two, 3: three, 4: four, 5: five}
Dart/Flutter Sort Map by Value
We can sort a Map by Value with following steps:
- get all Entries of the Map using
entries
property - convert the Entries into a List with toList() method
- sort the List using sort() method and compare function (for entries’ values)
- convert the sorted List into Map with fromEntries() method
Map map = {3: 'three', 1: 'one', 4: 'four', 5: 'five', 2: 'two'};
var sortedByValueMap = Map.fromEntries(
map.entries.toList()..sort((e1, e2) => e1.value.compareTo(e2.value)));
print(sortedByValueMap);
Output:
{5: five, 4: four, 1: one, 3: three, 2: two}
SplayTreeMap
We can use the SplayTreeMap to sort a Map. A SplayTreeMap
is a type of map which is based on a self-balancing binary tree, it automatically iterates keys in a sorted order.
Keys will be compared (for ordering) using the compare function passed in the constructor or from()
method. If we don’t specify the compare function, the objects are assumed to be Comparable, and are compared using Comparable.compareTo()
method.
Map map = {3: 'three', 1: 'one', 4: 'four', 5: 'five', 2: 'two'};
var sortedByKeyMap =
new SplayTreeMap<int, String>.from(map, (k1, k2) => k1.compareTo(k2));
/* same result */
// sortedByKeyMap = new SplayTreeMap<int, String>.from(map);
print(sortedByKeyMap);
Output:
{1: one, 2: two, 3: three, 4: four, 5: five}
If you want to use SplayTreeMap
to a Map by Value, just change the compare function.
Map map = {3: 'three', 1: 'one', 4: 'four', 5: 'five', 2: 'two'};
var sortedByValueMap = new SplayTreeMap<int, String>.from(
map, (k1, k2) => map[k1].compareTo(map[k2]));
print(sortedByValueMap);
Output:
{5: five, 4: four, 1: one, 3: three, 2: two}
Conclusion
Today we’ve known how to sort a Map in Dart/Flutter by key or by value.
There are many interesting things about Dart List and Map, please visit:
– Dart/Flutter List Tutorial with Examples
– Dart/Flutter Map Tutorial with Examples
– Dart/Flutter – Sort list of Objects
Happy Learning! See you again.
Further Reading
– Dart/Flutter Constructors tutorial with examples
– Dart/Flutter String Methods & Operators tutorial with examples
– Dart/Flutter Future Tutorial with Examples