Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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# 我可以使用Linq为C排序字典创建比较器吗_C#_Sorting_Dictionary_Icomparer - Fatal编程技术网

C# 我可以使用Linq为C排序字典创建比较器吗

C# 我可以使用Linq为C排序字典创建比较器吗,c#,sorting,dictionary,icomparer,C#,Sorting,Dictionary,Icomparer,有没有一种使用Linq创建SortedDictionary的方法?这将避免创建比较器类带来的不便(以及代码膨胀) 例如,创建按字符串键的反面排序的字典: //NOT VALID SYNTAX SortedDictionary<string, int> sortDict = new SortedDictionary(kvp => new String(kvp.Key.Reverse().ToArray()); //VALID SYNTAX SortedDictionary<

有没有一种使用Linq创建SortedDictionary的方法?这将避免创建比较器类带来的不便(以及代码膨胀)

例如,创建按字符串键的反面排序的字典:

//NOT VALID SYNTAX
SortedDictionary<string, int> sortDict = new SortedDictionary(kvp => new String(kvp.Key.Reverse().ToArray());

//VALID SYNTAX
SortedDictionary<string, int> sortDict = new SortedDictionary<string, int>(new ReverseStringComparer);

private class ReverseStringComparer: IComparer<String>
{
    public int Compare(string x, string y)
    {
        string s1 = new string(x.Reverse().ToArray());
        string s2 = new string(y.Reverse().ToArray());
        return s1.CompareTo(s2);
    }
}
//语法无效
SortedDictionary SORTDICTIONARY=新的SortedDictionary(kvp=>新字符串(kvp.Key.Reverse().ToArray());
//有效语法
SortedDictionary SORTDICTION=新的SortedDictionary(新的反向比较器);
私有类ReverseStringComparer:IComparer
{
公共整数比较(字符串x、字符串y)
{
字符串s1=新字符串(x.Reverse().ToArray());
字符串s2=新字符串(y.Reverse().ToArray());
返回s1.CompareTo(s2);
}
}

您可以定义一个通用比较器类,对要比较的项目应用提取函数:

public class KeyComparer<TItem, TKey> : Comparer<TItem>
{
    private readonly Func<TItem, TKey> extract;
    private readonly IComparer<TKey> comparer;

    public KeyComparer(Func<TItem, TKey> extract)
        : this(extract, Comparer<TKey>.Default)
    { }

    public KeyComparer(Func<TItem, TKey> extract, IComparer<TKey> comparer)
    {
        this.extract = extract;
        this.comparer = comparer;
    }

    public override int Compare(TItem x, TItem y)
    {
        // need to handle nulls
        TKey xKey = extract(x);
        TKey yKey = extract(y);
        return comparer.Compare(xKey, yKey);
    }
}
公共类键比较器:比较器
{
私有只读函数提取;
专用只读IComparer比较器;
公钥比较器(Func摘录)
:此(提取,比较器。默认值)
{ }
公钥比较器(Func extract、IComparer比较器)
{
this.extract=提取;
this.comparer=比较器;
}
公共覆盖整数比较(滴度x,滴度y)
{
//需要处理空值
txkey=extract(x);
TKey yKey=提取(y);
返回comparer.Compare(xKey,yKey);
}
}
我通常使用此类提取属性;但是,您可以定义任何函数,例如字符串反转:

SortedDictionary<string, int> sortDict = new SortedDictionary<string, int>(
    new KeyComparer<string, string>(s => new string(s.Reverse().ToArray())));
SortedDictionary sortDict=新的SortedDictionary(
新的键比较器(s=>新字符串(s.Reverse().ToArray());

更新:我已经在我的中详细介绍了这个比较器。

为什么不使用更有效的Jon Skeet?建议的ReverseStringComparer类反转每个字符串,然后进行比较。Jon Skeet的版本只反转排序顺序。@TimSchmelter我认为OP是在排序之前反转字符串,而不是而不是按相反的顺序排序。我不会为了使用LINQ而尝试使用LINQ。它只是一个工具,在适当的时候使用它。在我看来,这几行代码几乎不符合代码膨胀的条件。
LINQ
不会创建比较器。但是
LINQ
方法(如OrderBy)可以使用比较器。