How to sort map's values?

后端 未结 4 1076
心在旅途
心在旅途 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:08

    Something like this could work for you:

    Map chordtypes = {
      "maj": [0, 4, 7],
      "M7": [0, 4, 7, 11],
      "m7": [0, 3, 7, 10],
      "6": [0, 4, 7, 9],
      "9": [0, 4, 7, 10, 14],
      "sus2": [0, 2, 7],
      "sus4": [0, 5, 7],
      "omit3": [0, 7],
      "#5": [0, 4, 8],
      "+7b9#11": [0, 4, 8, 10, 13, 18],
      "+9": [0, 4, 8, 10, 14]
    };
    
    List keys = chordtypes.keys.toList();
    keys.sort((k1, k2) {
      if(chordtypes[k1].length > chordtypes[k2].length)
        return -1;
      if(chordtypes[k1].length < chordtypes[k2].length)
        return 1;
      return 0;
    });
    keys.forEach((String k) {
      print('$k ${chordtypes[k]}');
    });
    
    0 讨论(0)
  • 2021-01-06 12:14

    A function that does sort a Map of List on their length.

    import 'dart:collection';
    
    /// sorts the ListMap (== A Map of List<V>) on the length
    /// of the List values.
    LinkedHashMap sortListMap(LinkedHashMap map) {
        List mapKeys = map.keys.toList(growable : false);
        mapKeys.sort((k1, k2) => map[k1].length - map[k2].length);
        LinkedHashMap resMap = new LinkedHashMap();
        mapKeys.forEach((k1) { resMap[k1] = map[k1] ; }) ;        
        return resMap;
    }
    

    result for :

    var res = sortListMap(chordtypes);
    print(res);
    

    ==>

    { omit3: [0, 7], 
      maj: [0, 4, 7], 
      sus2: [0, 2, 7], 
      sus4: [0, 5, 7], 
      #5: [0, 4, 8], 
      M7: [0, 4, 7, 11], 
      m7: [0, 3, 7, 10], 
      6: [0, 4, 7, 9], 
      9: [0, 4, 7, 10, 14], 
      +9: [0, 4, 8, 10, 14], 
      +7b9#11: [0, 4, 8, 10, 13, 18] }
    
    0 讨论(0)
  • 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<int, Foo> map = //fill map
    
    var entries = map.entries.toList();
    entries.sort((MapEntry<int, Foo> a, MapEntry<int, Foo> b) => a.value.x.compareTo(b.value.x));
    map = Map<int, Foo>.fromEntries(entries);
    
    0 讨论(0)
  • 2021-01-06 12:22

    Building on @Leonardo Rignanese's answer. An extension function for a more functional approach:

    extension MapExt<T, U> on Map<T, U> {
      Map<T, U> sortedBy(Comparable value(U u)) {
        final entries = this.entries.toList();
        entries.sort((a, b) => value(a.value).compareTo(value(b.value)));
        return Map<T, U>.fromEntries(entries);
      }
    }
    

    General usage:

    foos.sortedBy((it) => it.bar);
    

    Usage for OP:

    final sortedChordtypes = chordtypes.sortedBy((it) => it.length);
    

    Gist here: https://gist.github.com/nmwilk/68ae0424e848b9f05a8239db6b708390

    0 讨论(0)
提交回复
热议问题