2014-02-22 90 views
1

我試圖根據我的字典值以升序輸出排序後的字典字典。這些價值正在蒸發,並不斷變化。但我沒有得到理想的輸出。根據字典值排序輸出排序列表

現在我有這樣的:

foreach(KeyValuePair<string, float> p in estimote_data.estimoteDictionary) 
{ 
    Debug.Log("Key Valeu: " + p.Key + " Value value: " + p.Value);   
} 

這是擺在我的更新方法,並輸出以下內容:

主要Valeu:6491值值:2.782559

的幾行Unity Xcode debug stuff

Key Valeu:18087 Value value:0.278256

我想知道它的值,所以我可以看到值根據Value值自動排序,但我努力完成這件事。有人可以幫我嗎?

回答

0

SortedDictionary按鍵排序。如果要按某種順序對項目列表進行排序,則需要告訴程序:

foreach(KeyValuePair<string, float> p in estimote_data.estimoteDictionary.OrderBy(p=>p.Value)) 
{ 
    Debug.Log("Key Valeu: " + p.Key + " Value value: " + p.Value);   
} 

此排序按照O(n log n)的順序很貴。如果您只使用字典進行排序,您可以簡單地切換鍵和值,如下所示:

foreach(KeyValuePair<float, string> p in estimote_data.estimoteDictionary) 
{ 
    Debug.Log("Key Valeu: " + p.Value+ " Value value: " + p.Key);   
}