Dart - How to sort Map's keys

前端 未结 3 1783
天命终不由人
天命终不由人 2020-12-11 14:47

I have question in sorting Map\'s key in Dart.

Map map = new Map();

How can

相关标签:
3条回答
  • 2020-12-11 14:58

    I know my answer is too late but check out what I've found.. Might help someone

    This sortedmap Package helps to maintain Map of objects in a sorted manner.

    0 讨论(0)
  • 2020-12-11 15:04

    If you want a sorted List of the map's keys:

    var sortedKeys = map.keys.toList()..sort();
    

    You can optionally pass a custom sort function to the List.sort method.

    Finally, might I suggest using Map<String, dynamic> rather than Map<String, Object>?

    0 讨论(0)
  • 2020-12-11 15:07

    In Dart, it's called SplayTreeMap:

    import "dart:collection";
    
    main() {
      final SplayTreeMap<String, Map<String,String>> st = 
          SplayTreeMap<String, Map<String,String>>();
    
      st["yyy"] = {"should be" : "3rd"};
      st["zzz"] = {"should be" : "last"};
      st["aaa"] = {"should be" : "first"};
      st["bbb"] = {"should be" : "2nd"};
    
      for (final String key in st.keys) {
        print("$key : ${st[key]}");
      }
    }
    
    // Output:
    // aaa : first
    // bbb : 2nd
    // yyy : 3rd
    // zzz : last
    
    0 讨论(0)
提交回复
热议问题