标签:
起初想用SortedDictionary做游戏中的排行榜,代码如下:
using UnityEngine; using System; using System.Collections; using System.Collections.Generic; public class CustomComparer<T> : IComparer<T> { Func<T, T, int> mComparerFunc; public CustomComparer(Func<T, T, int> comparer) { this.mComparerFunc = comparer; } public int Compare(T x, T y) { return mComparerFunc(x, y); } } public class SortedDictTest : MonoBehaviour { SortedDictionary<string, int> mLeaderboard; void Start() { mLeaderboard = new SortedDictionary<string, int>(new CustomComparer<string>((x, y) => mLeaderboard[x].CompareTo(mLeaderboard[y]))); mLeaderboard.Add("Jhon", 10); mLeaderboard.Add("Dark", 40); mLeaderboard.Add("Ellie", 20); foreach (var item in mLeaderboard) { Debug.Log("Name: " + item.Key + " Score: " + item.Value); } } }
结果就是unity死循环
当你get字典中的数值时,它会调用比较器。比较器里又调用了字典,造成死循环
而且这种用法还有一个问题,可排序字典是对key进行排序,操作时内部有类似二分查找的机制。
在做排行榜时又要按名称匹配,又要自动按分数排序,此时是两套排序机制,字典内部顺序混乱,查找速度反而更慢。
解决方法也是有的,使用双字典可以解决:
public class SortedDictTest : MonoBehaviour { Dictionary<string, int> mScoreDict; SortedDictionary<string, int> mLeaderboard; void Start() { mScoreDict = new Dictionary<string, int>(); mLeaderboard = new SortedDictionary<string, int>(new CustomComparer<string>((x, y) => mScoreDict[x].CompareTo(mScoreDict[y]))); mScoreDict.Add("Jhon", 10); mScoreDict.Add("Dark", 40); mScoreDict.Add("Ellie", 20); mLeaderboard.Add("Jhon", 10); mLeaderboard.Add("Dark", 40); mLeaderboard.Add("Ellie", 20); foreach (var item in mLeaderboard) { Debug.Log("Name: " + item.Key + " Score: " + item.Value); } } }
具体看数据量多少来权衡,直接使用List排序也未尝不可
标签:
原文地址:http://www.cnblogs.com/hont/p/5637864.html