簡體   English   中英

如果鍵是列表(在C#中),如何按鍵對字典排序?

[英]How to sort a dictionary by key, if key is a list (in 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"

再次:如果鍵實際上是一個列表,並且該排序由列表的第一個元素執行,然后由列表的第二個元素執行,那么我該如何按關鍵字對字典進行排序?

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

我建議創建一個包含三個值的struct ,並將其用作鍵。 原因是因為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);

如果您確實需要將其作為當前設置,則可以使用(測試代碼,需要對其進行調整)。 它與原始答案類似,只是列出完整列表(假設最多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

要將列表用作字典的鍵,您可以執行以下操作

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM