2017-02-09 27 views
0

我有一本字典(使用C#):如何按鍵排序字典,如果鍵是列表(在C#中)?

Dictionary<List<string>, string> dictData = new Dictionary<List<string>, string>(); 

在字典中已經有(例如)值:

key: {"3", "1", "45"}, value: "test value 1" 
key: {"1", "2", "45"}, value: "test value 2" 
key: {"11", "1", "45"}, value: "test value 3" 
key: {"1", "1", "45"}, value: "test value 4" 

關鍵是一個字符串列表,它會始終保持有至少兩個元素。我需要做的是按鍵對字典進行排序,或者更確切地說,按照列表的第一個元素進行排序,並作爲按列表的第二個元素排序的第二個標準。字符串實際上是一個數字,所以它們應該按數字排序(「3」應該小於「11」)。再次

key: {"1", "1", "45"}, value: "test value 4" 
key: {"1", "2", "45"}, value: "test value 2" 
key: {"3", "1", "45"}, value: "test value 1" 
key: {"11", "1", "45"}, value: "test value 3" 

:因此,對於上面的例子,我應該得到以下結果我如何可以排序字典的關鍵,如果該鍵實際上是一個列表和通過的第一個元素進行排序列表,然後是列表的第二個元素?

+8

我強烈建議不要使用'List'作爲字典 – Jonesopolis

+2

一鍵如果該鍵是一個數字,需要如此對待,你爲什麼要使用字符串列表? –

+1

除了不使用列表作爲字典鍵:如果你真的需要你可以使用'SortedDictionary'與自定義比較器? – Brandon

回答

0

如果你真的需要它作爲當前的設置,這個工作(測試代碼,這將需要進行調整)。它與原始答案類似,只是完整列表(假設最多3個)。經過測試,似乎正在工作。你將需要添加邏輯,如果它不具有所有3等,這僅僅是一個基礎的設置讓你去:

private void DoIt() 
    { 
     Dictionary<List<string>, string> test = new Dictionary<List<string>, string>(); 
     List<string> workerList = new List<string>() { "3", "1", "45" }; 
     test.Add(workerList, "test value 1"); 
     workerList = new List<string>() { "1", "2", "45" }; 
     test.Add(workerList, "test value 2"); 
     workerList = new List<string>() { "11", "1", "45" }; 
     test.Add(workerList, "test value 3"); 
     workerList = new List<string>() { "1", "1", "45" }; 
     test.Add(workerList, "test value 4"); 


     foreach(KeyValuePair<List<string>,string> kvp in test.OrderBy(x => int.Parse(x.Key[0])).ThenBy(y => int.Parse(y.Key[1])).ThenBy(z => int.Parse(z.Key[2]))) 
     { 
      Console.WriteLine("Key: " + kvp.Key[0].ToString() + "," + kvp.Key[1].ToString() + "," + kvp.Key[2].ToString() + " | " + "Value: " + kvp.Value.ToString()); 
     } 
    } 

輸出:

Key: 1,1,45 | Value: test value 4 
Key: 1,2,45 | Value: test value 2 
Key: 3,1,45 | Value: test value 1 
Key: 11,1,45 | Value: test value 3 
4

如果您有兩個List<int>包含:1,2,5和1,2,5,那麼這兩個列表不是相同的列表。它們是單獨的列表實例,它們碰巧包含相同順序的相同值,因爲列表(像其他集合類型,包括陣列)是引用類型。您不能將它們用作唯一鍵,因爲字典會將它們視爲不同的鍵。

我建議建立一個struct包含您的三個值,並將它作爲重點。原因是因爲結構是一個值類型,並且具有相同屬性值的兩個實例將被視爲相等,這是字典密鑰所需的。

struct Values 
{ 
    public int First { get; set; } 
    public int Second { get; set; } 
    public int Third { get; set; } 
} 

然後,你可以這樣做:

var x = new Dictionary<Values, string>() 
    { 
     {new Values() {First = 1, Second = 1, Third = 45}, "test value 1"}, 
     {new Values() {First = 1, Second = 2, Third = 45}, "test value 2"}, 
     {new Values() {First = 11, Second = 1, Third = 45}, "test value 3"}, 
    }; 

var sorted = x.OrderBy(kvp => kvp.Key.First).Select(kvp => kvp.Value); 
0

要使用列表作爲一個辭典的鍵,你可以做這樣的事情

public class DictionaryKeyList { 

    public List<string> Lst { get; set; } 

    public override bool Equals(Object otherObj){ 
     var otherList = otherObj as DictionaryKeyList; 

     return !this.Lst.Zip(otherList, (a,b) => a == b).Any(x => !x); 
    } 

然後使用字典類型

Dictionary<DictionaryKeyList, string> dictData;