How to sort map's values?

后端 未结 4 1079
心在旅途
心在旅途 2021-01-06 11:45

Can someone give me a hint? I want to sort a map\'s values by the length of the lists.

var chordtypes = {
  \"maj\": [0, 4, 7],
  \"M7\": [0, 4, 7, 11],
  \"         


        
4条回答
  •  星月不相逢
    2021-01-06 12:19

    Using DART language:

    Let's say you want to sort a Map with integer key and value of type Foo:

    class Foo {
      int x; //it can be any type
    }
    

    So you can get the list of all the entries, sort them like a normal list and then rebuild the map:

    Map map = //fill map
    
    var entries = map.entries.toList();
    entries.sort((MapEntry a, MapEntry b) => a.value.x.compareTo(b.value.x));
    map = Map.fromEntries(entries);
    

提交回复
热议问题