Skip to main content

Flutter Dart Sort Map by Value Alphabetically

To sort Map by its value in ascending order we will use Map.fromEntries() function and pass value object inside it. When we pass value object then it will compare all the values and index them alphabetically.

Flutter Dart Sort Map by Value Alphabetically

void main() {
  
Map testMap = {10: 'T', 8: 'E', 6: 'S', 4: 'F', 2: 'T', 9: 'N', 7: 'S', 5: 'F', 3: 'T', 1: 'O'};

var sortedMap = Map.fromEntries(
    testMap.entries.toList()..sort((e1, e2) => e1.value.compareTo(e2.value)));

print(sortedMap);
  
}
Output:
Flutter Dart Sort Map by Value Alphabetically

Comments