根据字典值按顺序输出排序列表

N0xus

我正在尝试根据字典值以升序输出排序后的字典字典。这些价值正在不断涌现并不断变化。但是我没有得到期望的结果。

现在我有这个:

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

这放置在我的更新方法中,并输出以下内容:

关键值:6491值:2.782559

几行Unity Xcode调试内容

关键值:18087值:0.278256

我正在尝试获取它,以便可以看到这些值会根据Value值自动对自己进行排序,但是我正在努力做到这一点。有人可以帮我吗?

彼德

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);         
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章