{

Dart/Flutter: How to Sort Map Key and Values with examples


Map is an data structure with key and values in unordered way.

This tutorials shows multiple ways to sort map by key and values in Dart and Flutter.

One of the way is using collections package contains SplayTreeMap class to store items in sorted order.

How to sort Map by Values in dart and flutter?

There are multiple ways to sort keys in a Map SplayTreeMap class provides named constructor using from method that accepts map and function. function contains logic to sort keys each with other keys in a map.

SplayTreeMap.from(map,function)

Here is an example program to sort keys in ascending and descending order.

import 'dart:collection';

void main() {
  var numbers = {'one': 1, 'two': 2, 'three': 3, 'four': 4};
  print(numbers); //{one: 1, two: 2, three: 3, four: 4}

  // Sort Map key in ascending order
  final sortedKeysAsc = SplayTreeMap<String, dynamic>.from(
      numbers, (keys1, keys2) => keys1.compareTo(keys2));
  print(sortedKeysAsc); //{four: 4, one: 1, three: 3, two: 2}
  
  // Sort Map key in descending order
  final sortedKeysDesc = SplayTreeMap<String, dynamic>.from(
      numbers, (keys1, keys2) => keys2.compareTo(keys1));
  print(sortedKeysDesc); //{two: 2, three: 3, one: 1, four: 4}
}

Output:

{one: 1, two: 2, three: 3, four: 4}
{four: 4, one: 1, three: 3, two: 2}
{two: 2, three: 3, one: 1, four: 4}

How to sort map by value in dart and flutter?

This example sort map values in ascending and descending order.

if values are compared using numbers[keys1].compareTo(numbers[keys2]) It throws an Error: Method ‘compareTo’ cannot be called on ‘int?’ because it is potentially null. The reason is about null value safety. We have to use numbers[keys1]!.compareTo(numbers[keys2]!)

Here is an example for SplayTreeMap sort key and values

import 'dart:collection';

void main() {
  var numbers = {'one': 1, 'two': 2, 'three': 3, 'four': 4};
  print(numbers); //{one: 1, two: 2, three: 3, four: 4}

  // Sort Map key in ascending order
  final sortedValuesAsc = SplayTreeMap<String, int>.from(
      numbers, (keys1, keys2) => numbers[keys1]!.compareTo(numbers[keys2]!));
  print(sortedValuesAsc); //{one: 1, two: 2, three: 3, four: 4}

  
  // Sort Map key in descending order
  final sortedValuesDesc = SplayTreeMap<String, dynamic>.from(
      numbers, (keys1, keys2) => numbers[keys2]!.compareTo(numbers[keys1]!));
  print(sortedValuesDesc); //{four: 4, three: 3, two: 2, one: 1}

}

Output:

{one: 1, two: 2, three: 3, four: 4}
{one: 1, two: 2, three: 3, four: 4}
{four: 4, three: 3, two: 2, one: 1}

Conclusion

You’ve learned how to sort map based on key and values in dart and flutter programming.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example