我有一個字典<>我想根據價值排序,所以我已經通過將字典放入列表<>然後使用.Sort方法。排序字典<>值,從關鍵字查找索引
我已經添加回到詞典<>。是否可以通過使用Dictionary鍵來查找新的索引/順序?
Dictionary<int, MyObject> toCompare = new Dictionary<int, MyObject>();
toCompare.Add(0, new MyObject());
toCompare.Add(1, new MyObject());
toCompare.Add(2, new MyObject());
Dictionary<int, MyObject> items = new Dictionary<int, MyObject>();
List<KeyValuePair<int, MyObject>> values = new List<KeyValuePair<int, MyObject>> (toCompare);
// Sort.
values.Sort(new MyComparer());
// Convert back into a dictionary.
foreach(KeyValuePair<int, PropertyAppraisal> item in values)
{
// Add to collection.
items.Add(item.Key, item.Value);
}
// THIS IS THE PART I CAN'T DO...
int sortedIndex = items.GetItemIndexByKey(0);
不是詞典的順序在C#defenition未定義? – Wouter 2010-03-26 15:46:12
你想要做什麼?如果您需要獨特的集合,請使用字典。如果您需要排序列表中項目的索引,請使用SortedList。 – 2010-03-26 15:46:41
@Michael Todd:'SortedList'不是正確的解決方案。他希望根據集合中*值*的自定義比較來訂購商品。 – 2010-03-26 15:57:49