Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 排序列表<;keyValuePair<;字符串,字符串>&燃气轮机;_C#_.net - Fatal编程技术网

C# 排序列表<;keyValuePair<;字符串,字符串>&燃气轮机;

C# 排序列表<;keyValuePair<;字符串,字符串>&燃气轮机;,c#,.net,C#,.net,我在C#.net2.0 我有一个包含两个字符串的列表,我想对它进行排序。 列表类似于list 我必须根据第一个字符串对其进行排序,即: 行政协调会 阿布拉 南部 弗洛 IHNJ 我试图使用Sort(),但它给了我一个异常:“无效操作异常”,“无法比较数组中的两个元素” 您能建议我使用哪种方法来执行此操作吗?由于您一直使用.NET 2.0,您必须创建一个实现IComparer的类,并将其实例传递给排序方法: public class KvpKeyComparer<TKey, TValue

我在
C#
.net2.0 我有一个包含两个字符串的列表,我想对它进行排序。 列表类似于
list

我必须根据第一个
字符串对其进行排序,即:

  • 行政协调会
  • 阿布拉
  • 南部
  • 弗洛
  • IHNJ
我试图使用
Sort()
,但它给了我一个异常:“无效操作异常”,“无法比较数组中的两个元素”


您能建议我使用哪种方法来执行此操作吗?

由于您一直使用.NET 2.0,您必须创建一个实现
IComparer
的类,并将其实例传递给
排序
方法:

public class KvpKeyComparer<TKey, TValue> : IComparer<KeyValuePair<TKey, TValue>>
    where TKey : IComparable
{
    public int Compare(KeyValuePair<TKey, TValue> x,
                       KeyValuePair<TKey, TValue> y)
    {
        if(x.Key == null)
        {
            if(y.Key == null)
                return 0;
            return -1;
        }

        if(y.Key == null)
            return 1;

        return x.Key.CompareTo(y.Key);
    }
}

list.Sort(new KvpKeyComparer<string, string>());

为什么不改用SortedDictionary呢

以下是MSDN的相关文章:


您可以使用
比较
通用委托。然后,您就不需要仅仅为了实现
IComparer
而定义类,而只需要确保您定义的方法与委托签名相匹配

private int CompareByKey(KeyValuePair<string, string>, KeyValuePair<string, string> y)
{
    if (x.Key == null & y.Key == null) return 0;
    if (x.Key == null) return -1;
    if (y.Key == null) return 1;

    return x.Key.CompareTo(y.Key);
}

list.Sort(CompareByKey);
private int CompareByKey(KeyValuePair,KeyValuePair y)
{
如果(x.Key==null&y.Key==null)返回0;
if(x.Key==null)返回-1;
如果(y.Key==null)返回1;
返回x.Key.CompareTo(y.Key);
}
list.Sort(CompareByKey);
列表对=新列表();
添加(新的KeyValuePair(“维尔纽斯”、“阿尔及达斯”);
添加(新的KeyValuePair(“Trakai”、“Kestutis”);
Sort(委托(KeyValuePair x,KeyValuePair y){return x.Key.CompareTo(y.Key);});
foreach(成对变量对)
控制台写入线(对);
Console.ReadKey();

你能告诉我如何制作一个类IComparer或任何我可以轻松找到的教程吗。
private int CompareByKey(KeyValuePair<string, string>, KeyValuePair<string, string> y)
{
    if (x.Key == null & y.Key == null) return 0;
    if (x.Key == null) return -1;
    if (y.Key == null) return 1;

    return x.Key.CompareTo(y.Key);
}

list.Sort(CompareByKey);
List<KeyValuePair<string, string>> pairs = new List<KeyValuePair<string, string>>();
pairs.Add(new KeyValuePair<string, string>("Vilnius", "Algirdas"));
pairs.Add(new KeyValuePair<string, string>("Trakai", "Kestutis"));

pairs.Sort(delegate (KeyValuePair<String, String> x, KeyValuePair<String, String> y) { return x.Key.CompareTo(y.Key); });
foreach (var pair in pairs)
     Console.WriteLine(pair);
Console.ReadKey();