在C#中比较List <KeyValuePair <>>

时间:2019-11-27 10:39:03

标签: c# .net list

我正在开发一个程序,该程序需要保存一个乐谱和字符串列表,我相信List<KeyValuePair<int, List<string>>>将是最好的列表。我不能使用字典,因为键不是唯一的,所以我使用Insert和BinarySearch来使列表保持排序,并且我不想在每次插入后使用List.Sort,因为我要添加很多项目。 code在我的代码中,我有:

private List<KeyValuePair<int, List<string>>> list;

List<string> values = new List<string>
{
    value1,
    value2,
    value3
};

// Insert values
list.Insert(index, new KeyValuePair<int, List<string>>(score, values));

// Find index of new position
int index = list.BinarySearch(new KeyValuePair<int, List<string>>(score, values), new Comparer());

private class Comparer : IComparer<KeyValuePair<int, List<string>>>
{
    public int Compare(
        KeyValuePair<int, List<string>> x,
        KeyValuePair<int, List<string>> y)
    {
        if ((x.Key == y.Key) && (x.Value[0] == y.Value[0]))
            return 0;
        if ((x.Key < y.Key) || ((x.Key == y.Key) && (x.Value[0] != y.Value[0])))
            return 1;

        return -1;
    }
}

“列表”按得分降序维护,BinarySearch检查得分和第一个值是否不在列表中或返回其在列表中的位置,但我无法使Comparer正常工作。列表示例为:

23, Harry, Ottawa, Green 17, Amy, Venice, Red 17, Sue, Sydney, Blue 4, Harry, Durban, Blue

在此示例中,4, Harry, Miami, Red将无效,因为4, Harry...已经存在。插入9, Dallas...将返回3作为新位置。

但是我在课堂上遇到错误CS0535 'Comparer' does not implement interface member 'IComparer<KeyValuePair<int, List<string>>>.Compare(KeyValuePair<int, List<string>>, KeyValuePair<int, List<string>>)'。我在做什么错了,我该如何解决?

2 个答案:

答案 0 :(得分:1)

这似乎是值的第一个元素具有特殊含义。

整数值与第一个元素是唯一的组合,您可以使用具有该整数和列表的第一个元素的元组的Dictionary作为键:

Try the below code yourself

// A SortedDictionary is like a Dictionary, but automatically sorted on the key
var dict = new SortedDictionary<(int, string), List<string>>();

var key = (23, "Harry");
// If you are using .NET Core 2.0 or above, you can use
/*
 * if (!dict.TryAdd(key, new List<string> { "Ottawa", "Green" }))
 *     Console.WriteLine($"Can't add {key}");
 */
if (!dict.ContainsKey(key))
    dict.Add(key, new List<string> { "Ottawa", "Green" });
else
    Console.WriteLine($"Can't add {key}");

key = (4, "Harry");
if (!dict.ContainsKey(key))
    dict.Add(key, new List<string> { "Durban", "Blue" });
else
    Console.WriteLine($"Can't add {key}");

key = (4, "Harry");
if (!dict.ContainsKey(key))
    dict.Add(key, new List<string> { "this is", "duplicate" });
else
    Console.WriteLine($"Can't add {key}");

key = (17, "Amy");
if (!dict.ContainsKey(key))
    dict.Add(key, new List<string> { "Venice", "Red" });
else
    Console.WriteLine($"Can't add {key}");

key = (17, "Sue");
if (!dict.ContainsKey(key))
    dict.Add(key, new List<string> { "Sydney", "Blue" });
else
    Console.WriteLine($"Can't add {key}");

Console.WriteLine("---------------------------------------");
Console.WriteLine("Notice now how sorted is the dictionary");
Console.WriteLine("---------------------------------------");
foreach (var Key in dict.Keys)
{
    Console.WriteLine($"dict[{Key}] Contains : ");
    foreach (var val in dict[Key])
    {
        Console.WriteLine($"\t{val}");
    }
}

这将输出:

Can't add (4, Harry)
---------------------------------------
Notice now how sorted is the dictionary
---------------------------------------
dict[(4, Harry)] Contains : 
    Durban
    Blue
dict[(17, Amy)] Contains : 
    Venice
    Red
dict[(17, Sue)] Contains : 
    Sydney
    Blue
dict[(23, Harry)] Contains : 
    Ottawa
    Green

答案 1 :(得分:0)

根据错误消息判断,new Comparer()中的int index = list.BinarySearch(new KeyValuePair<int, List<string>>(score, values), new Comparer());不会初始化private class Comparer : IComparer<KeyValuePair<int, List<string>>>,而是初始化System.Collections.Comparer。您应该像这样向其添加名称空间:{{ 1}}