2014-02-24 73 views
0

我有以下字典我填寫了通過第三方對象解析的數據。詞典列表問題

它看起來像這樣這是我的解析類:

public string beaconMajor; 
public string[] beaconValues; 
public Dictionary<string, float> valueDictionary = new Dictionary<string, float>(); 
public string foo = ""; 

public void DistanceValue(string message) 
{ 

    foo = message.Split (','); 

    foreach (string b in foo) 
    { 
     beaconValues = beacon.Split('_'); 

     beaconMajor = beaconValues[0]; 
     string beaconDistance = beaconValues[1]; 

     if(valueDictionary.ContainsKey(beaconMajor)) 
     { 
      valueDictionary[beaconMajor] = float.Parse(beaconDistance); 
     } 
     else 
     { 
      valueDictionary.Add(beaconMajor, float.Parse(beaconDistance)); 
     } 
    } 

    if (foo.Contains("-1")) 
    { 
     return; 
    } 
} 

然後在另一個類我在做與字典以下,只會向你展示一個條目,但也有不少,看起來像這樣:

if (foo_class.valueDictionary.ContainsKey ("key")) 
{ 
    beacon1Scale = float.Parse (foo_class.valueDictionary["key"].ToString()); 
} 

現在我想要做的是命令這個詞典的價值,而不是關鍵。這些鍵是常量,但每隔幾秒鐘更改一次。我試過下面的代碼:

List<KeyValuePair<string, float>> list = foo_class.valueDictionary.ToList(); 

list.Sort 
(delegate(KeyValuePair<string, float> val1, KeyValuePair<string, float> val2) 
    { 
     return val1.Value.CompareTo(val2.Value); 
    } 
); 

Debug.Log(list); 

dictionaryString = list.ToString() + "\n"; 

但是當我運行它,我得到在Xcode以下消息:

System.Collections.Generic.List 1[System.Collections.Generic.KeyValuePair 2 System.String,系統。單] UnityEngine.Debug:Internal_Log(的Int32,字符串,對象) UnityEngine.Debug:日誌(對象)TriangulationScript:更新()

可有人請告訴我這個消息的原因是什麼?

我還在尋找到讓這本字典通過值自行解決,我甚至使用LINQ嘗試,但,這是在Xcode不相容的,我是在Xcode中JIT錯誤信息如下:

ExecutionEngineException:嘗試JIT編譯方法 'System.Linq.OrderedEnumerable1:GetEnumerator ()',同時使用--aot-only運行。

如果有人知道任何其他方式來做到這一點,請讓我知道。我已經花了幾天時間了。

更新

我用下面所建議的答案,並在xcode中給出這樣的信息:

ExecutionEngineException:試圖JIT編譯方法 「System.Linq.OrderedEnumerable 1<System.Collections.Generic.KeyValuePair 2>:的GetEnumerator() '而運行 - 只 - 只。

在 System.Collections.Generic.List 1[System.Collections.Generic.KeyValuePair 2 [System.String,System.Single]] AddEnumerable (IEnumerable的1 enumerable) [0x00000] in <filename unknown>:0 at System.Collections.Generic.List 1 [System.Collections.Generic.KeyValuePair 2[System.String,System.Single]]..ctor (IEnumerable 1集合)[0x00000]中:0 at System.Linq.Enumerable.ToList [KeyValuePair​​1 source) [0x00000] in:0 at TriangulationScript。更新() [0x00000]中:0

+0

回滾編輯需要在xCode中編譯上述代碼,這就是爲什麼我不能使用LINQ。如果有人知道這樣做的方式,請告訴我。 – N0xus

+0

OT:你如何在xcode中使用c#? – Teejay

+0

xCode插入Unity。我所有的C#都是Unity的一面。 – N0xus

回答

0

嘗試是這樣的:

List<KeyValuePair<string, float>> list = items 
              .OrderBy(x => x.Value) 
              .ToList<KeyValuePair<string, float>>(); 
+0

OrderBy是Linq,我以前用過。這是什麼導致了JIT消息隊友。 – N0xus

+0

然後,嘗試使用我的github回購的一些排序算法。 https://github.com/Oscarbralo/Algorithms 但是你需要用它們來與字典;) –

0

list.ToString()不會做你認爲它 - 它看起來像你期待list.ToString()輸出整個列表的內容爲一個字符串。相反,它使用的默認行爲object.ToString()這是發射物體的完全限定類型名稱 - 在這種情況下是

System.Collections.Generic.List`1[System.Collections.Generic.KeyValuePair`2[System.String,System.Single]] 

我還在尋找到讓這本字典通過值自行解決。

一個Dictionary訂單 - 如果你想輸出按照一定的順序凱/值對,你必須每次遍歷列表時指定的順序,或作爲存儲一個鍵/值對列表,它不會有Dictionary特定的方法可用(如索引查找)