2013-12-15 46 views
0

我有一個SortedDictionary其中密鑰是對類商店的引用。值爲List<Item>如何使用鍵和值一起對SortedDictionary進行排序?

我想排序字典中的商店。 IComparer只能應用於Key的值。

但是,對於正確的排序,需要Value(List<Item>)中的數據。價格爲例。

我該怎麼做? 使用SortedDictionary可選,我會處理任何決定。 重要的是,我可以快速獲得鑰匙的價值,類似於TryGetValue

class Item 
{ 
    int price; 
    string name; 
} 

class Store 
{ 
    string owner; 
    string street; 
    string city; 
} 

SortedDictionary<Store, List<Item>> 
+0

也許'SortedDictionary <商店,SortedSet的>'? –

+0

不,我需要對商店的列表/字典進行排序。使用貨物價格的數據。 – Mixer

+0

您可以將'Store'類的'List '部分作爲字段,然後您就可以按照自己想要的方式對商店列表進行排序。 – Yuriy

回答

0

那麼你可以進行排序使用LINQ擴展方法像OrderByOrderByDescending存儲列表:

Dictionary<Store, List<Item>> stores; 
... 
var result = stores.OrderBy(pair => pair.Value.Min(item => item.price)); 
相關問題