Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a Map by its value in dart?

Tags:

dart

I want to sort a Map<String,int> by values.

{'201': 4, '2017CS197': 2, '2017CS300': 6, '202': 4, '205': 3, '206': 3, '207': 5}

this is my map.

after i want to get result as {'2017CS300': 6,'207':5,'202':4,'201':4,'206':3,'205':3,'2017CS197':2}

Thank You!

like image 496
awpathum Avatar asked May 09 '19 17:05

awpathum


People also ask

What is a map in Dart?

A map is a collection of key/value pairs. The value is retrieved from a map with its associated key. Maps are also called dictionaries, associative arrays, or hash tables. Depending on the iteration order, there are three types of maps in Dart:

How to retrieve the value from a map in Dart?

The value is retrieved from a map with its associated key. Maps are also called dictionaries, associative arrays, or hash tables. Depending on the iteration order, there are three types of maps in Dart: By default, creating an instance using Map constructor ( Map, Map.from, or Map.of) creates a LinkedHashMap .

How to sort a map in C++?

By default, a Map in C++ is sorted in increasing order based on its key. Below is the various method to achieve this: Method 1 – using the vector of pairs The idea is to copy all contents from the map to the corresponding vector of pairs and sort the vector of pairs according to second value using the lambda function given below:

How do you write a map literal in Dart?

Dart Map literal A map literal consists of a pair of curly brackes, in which we specify the key/value pairs. The pairs are separated by comma. The key is separated from the value by colon.


2 Answers

The default map class is insertion-ordered on the keys. The easiest approach is to just create a new map with the same entries, added in the order you want. First, let's find the order you want:

var sortedEntries = map.entries.toList()..sort((e1, e2) {
  var diff = e2.value.compareTo(e1.value);
  if (diff == 0) diff = e2.key.compareTo(e1.key);
  return diff;
});

(This assumes that your keys and values are all Comparable, otherwise you need to figure out how to compare them yourself. In this particular case, where keys are Strings and values are ints, it's only null keys or values that may be incomparable).

This orders the entries in reverse value order, and for equal values, in reverse key order.

You can then create a new map from those entries:

var newMap = Map<String, int>.fromEntries(sortedEntries);

or you can modify the existing map by removing the old entries and adding the new:

map..clear()..addEntries(sortedEntries)

or

for (var entry in sortedEntries) {
  map..remove(entry.key)..[entry.key] = entry.value;
}

There is no functionality on the map class itself to order its entries.

like image 80
lrn Avatar answered Oct 04 '22 23:10

lrn


To sort a map by its value in ascending order in Dart :

Map<String, int> map = {'one': 10, 'two': 5, 'three': 7, 'four': 0};

var mapEntries = map.entries.toList()
  ..sort((a, b) => a.value.compareTo(b.value));

map
  ..clear()
  ..addEntries(mapEntries);

print(map); // Must print: {four: 0, two: 5, three: 7, one: 10}
like image 42
Adrien Arcuri Avatar answered Oct 04 '22 23:10

Adrien Arcuri