flutter中如何实现map对象按key值排序类似java中的SortedMap对象?

码云
2020-10-28 15:21

flutter/dart中提供了类似java的SortedMap对象,会根据map的key值排序。参考以下示例代码:

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

 

全部评论

相关文章