2013-08-15 13 views

risposta

5

Se si desidera un ordinato List delle chiavi della mappa:

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

Opzionalmente si può passare una funzione di ordinamento personalizzata al metodo List.sort.

Infine, potrei suggerire di utilizzare Map<String, dynamic> anziché Map<String, Object>?

+0

Grazie del tuo suggerimento! –

+1

Attenzione per one-liner 'map.keys.toList(). Sort()'. Per mantenere questo nella stessa riga, dovrebbe essere usato lo stile a cascata, come in 'map.keys.toList() .. sort()'. – nunobaba

0

Utilizzare un TreeMap. Questo dovrebbe risolvere il tuo problema.

 Map<String, String> unsortMap = new HashMap<String, String>(); 
     unsortMap.put("2", "B"); 
     unsortMap.put("1", "A"); 
     unsortMap.put("4", "D"); 
     unsortMap.put("3", "B"); 

     Map<String, String> treeMap = new TreeMap<String, String>(unsortMap); 

Spero che questo aiuti.

+0

Non c'è put e non TreeMap? – Gero

8

In Dart Si chiama SplayTreeMap:

import "dart:collection"; 
main() { 
    var st = new SplayTreeMap<String, Object>(); 
    st["yyy"]={"should be":"3rd"}; 
    st["zzz"]={"should be":"last"}; 
    st["aaa"]={"should be":"first"}; 
    st["bbb"]={"should be":"2nd"}; 
    for (var key in st.keys) { 
    print("$key : ${st[key]['should be']}"); 
    } 
} 
//Output: 
//aaa : first 
//bbb : 2nd 
//yyy : 3rd 
//zzz : last 
Problemi correlati