2010-05-14 84 views
3

看起來SortedList被排除在外..您認爲最適合現有集合之外的什麼? (保持相同的快速訪問和更改)SortedList移植到Silverlight

silverlight字典可以永久排序嗎?

問候 VLK

回答

0

要回答你的問題:第字典裏沒有重要保障訂購的商品。

但是List<T>支持BinarySearch方法。這可以幫助你實現SortedList的類似目標。

該文檔有一個很好的例子,說明這基本上是如何實現的。請參閱: -

List<T>.BinarySearch Method (T)

+0

謝謝,還是文檔說: 列表必須按照比較器的實現進行排序;否則,結果不正確。 – VLK 2010-05-14 17:57:17

+0

啊,你的想法是保持列表總是按照BinarySearch給出的位置插入。 hm,我將檢查插入時間對集合大小的依賴性。 – VLK 2010-05-14 18:01:08

0

你可以看看在Wintellect的Power Collections。這個庫在EPL下是免費的。我認爲OrderedMultiDictionary會爲你解決問題。你應該可以在Silverlight下編譯它。我已經使用了幾年,但是使用這個庫(和Richter的線程庫)來進行WPF項目。

編輯:

玩弄這個看起來你需要做一些工作來獲得動力集合,以在Silverlight後做事。

0

我沒有silverlight的解決方法是用KeyValuePair類型創建一個通用列表,然後對列表進行排序。

List<KeyValuePair<int, string>> sampleList = new List<KeyValuePair<int, string>>(); 

//Assuming you have a set of objects in an array or list 
foreach(var item in items) 
{ 
    sampleList.Add(new KeyValuePair<int, string>>(item.ID, item.Description)) 
} 

sampleList = sampleList.OrderBy(data => data.Key).ToList(); 

效果與使用排序列表相同。