To sort a map by values in Dart, you can convert the map to a list of key-value pairs, sort the list by values, and then convert it back to a map. This method provides a straightforward way to sort the map based on values.
We can sort a map by values in Dart by converting the map to a list of key-value pairs, sorting the list by values, and then converting it back to a map.
For example,
myMap
with some key-value pairs. In this example, the map has string keys and integer values.entries.toList
method.sort
method with a custom comparator.Map.fromEntries
method.print
function to verify the sorting.void main() {
// Declare and initialize a map
Map<String, int> myMap = {
'apple': 3,
'banana': 1,
'cherry': 2
};
// Convert the map to a list of key-value pairs
var sortedEntries = myMap.entries.toList()
..sort((a, b) => a.value.compareTo(b.value));
// Create a new map from the sorted list of key-value pairs
Map<String, int> sortedMap = Map.fromEntries(sortedEntries);
// Print the sorted map
print('Sorted Map by Values:');
sortedMap.forEach((key, value) => print('$key: $value'));
}
Sorted Map by Values: banana: 1 cherry: 2 apple: 3
We can sort a map by values in descending order in Dart by converting the map to a list of key-value pairs, sorting the list by values in descending order, and then converting it back to a map.
For example,
myMap
with some key-value pairs. In this example, the map has string keys and integer values.entries.toList
method.sort
method with a custom comparator.Map.fromEntries
method.print
function to verify the sorting.void main() {
// Declare and initialize a map
Map<String, int> myMap = {
'apple': 3,
'banana': 1,
'cherry': 2
};
// Convert the map to a list of key-value pairs
var sortedEntries = myMap.entries.toList()
..sort((a, b) => b.value.compareTo(a.value));
// Create a new map from the sorted list of key-value pairs
Map<String, int> sortedMap = Map.fromEntries(sortedEntries);
// Print the sorted map
print('Sorted Map by Values (Descending):');
sortedMap.forEach((key, value) => print('$key: $value'));
}
Sorted Map by Values (Descending): apple: 3 cherry: 2 banana: 1
We can sort a map with numeric keys by values in Dart by converting the map to a list of key-value pairs, sorting the list by values, and then converting it back to a map.
For example,
myMap
with some key-value pairs. In this example, the map has integer keys and string values.entries.toList
method.sort
method with a custom comparator.Map.fromEntries
method.print
function to verify the sorting.void main() {
// Declare and initialize a map
Map<int, String> myMap = {
3: 'three',
1: 'one',
2: 'two'
};
// Convert the map to a list of key-value pairs
var sortedEntries = myMap.entries.toList()
..sort((a, b) => a.value.compareTo(b.value));
// Create a new map from the sorted list of key-value pairs
Map<int, String> sortedMap = Map.fromEntries(sortedEntries);
// Print the sorted map
print('Sorted Map by Values:');
sortedMap.forEach((key, value) => print('$key: $value'));
}
Sorted Map by Values: 1: one 2: two 3: three
In this tutorial, we learned How to Sort a Map by Values in Dart language with well detailed examples.