{

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: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example