-1

I have a the following Map:

Map <String,Map<String,dynamic>> _allData = {
   "jewishStudies": {'jewish': ['b2019','a2019']},

   "socialScience": {'social1': ['a2017','c2014','b2020'],
                     'social2': ['a2012','c2015','b2011'],
                     'social3': ['a2010','c2008','b2005']},

   "humanities":    {'human': ['z2017','c2014','k2020']},

   "exactSciences": {'exact': ['d2017','c2014','c2020']},

   "engineering":   {'eng': ['a2017']},

   "lifeScience":   {'life1': ['y2017','c2014','d2020'],
                     'life2': ['t2017','t2014','s2020'],
                     'life3': ['e2017','c2014','b2020']},

  "interStudies":  {'inter1':['a2017','c2014','b2020'],
                    'inter2':['a2017','c2014','b2020']},

   "general":      {'gen': ['g2017','w2014','b2020']},
  };

I want to do:

  1. sort the enteries of the external map : Map <String,Map> by alphabetical order of the keys of that map (which there type is String).

  2. sort the enteries of the internal map : Map<String,dynamic> by alphabetical order of the keys of that map (which there type is String).

  3. sort the items in the List of the internal map : Map by alphabetical order.

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
einav
  • 145
  • 1
  • 2
  • 10
  • What do you mean by sort the map? You can't access the map value by the order it's stored in memory or from an index, you can only retrieve a value using its associated key. – Christopher Moore May 21 '20 at 22:40
  • i want to change the order of the entries in the two maps such that the keys orderd by alphabetical order, and the items in the list was orderd by alphabetical order too. – einav May 21 '20 at 22:52

1 Answers1

0

You can use SplayTreeMap which iterates the keys in sorted order.

An example from this question(credit to Ilya Kharlamov):

import "dart:collection";
main() {
  SplayTreeMap st = new SplayTreeMap<String, dynamic>();
  st["yyy"]={"should be":"3rd"};
  st["zzz"]={"should be":"last"};
  st["aaa"]={"should be":"first"};
  st["bbb"]={"should be":"2nd"};
  for (String key in st.keys) {
    print("$key : ${st[key]}");
  }
}
//Output:
//aaa : first
//bbb : 2nd
//yyy : 3rd
//zzz : last
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52