C# 使用字符串实现自定义 IComparer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8975698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:24:54  来源:igfitidea点击:

Implementing custom IComparer with string

c#icomparer

提问by maxp

I have a collection of strings in c#, for example;

例如,我在 C# 中有一个字符串集合;

var example = new string[]{"c", "b", "a", "d"};

I then with to sort this, but my IComparer method is not working, and looping infinitely by the seems of things.

然后我对此进行了排序,但是我的 IComparer 方法不起作用,并且看起来无限循环。

Basically I need "b"to come first, followed by "c", then I dont care about the order of any of the others.

基本上我需要"b"先来,然后是"c",然后我不关心任何其他人的顺序。

Is this possible using IComparer<string>and the Compare(string x, string y)method?

这可以使用 IComparer<string>Compare(string x, string y)方法吗?

Edit: Code

编辑:代码

    public int Compare(string x, string y)
    {
        var sOrder = new string[] { "b", "c" };
        int index_x = -1;
        int index_y = -1;

        for (int i = 0; i < sOrder.Length;i++)
        {
            if (sOrder[i] == x)
                index_x = i;
            else if (sOrder[i] == y)
                index_y = i;
        }

        if (index_x >= 0 && index_y >= 0)
        {
            if (index_x < index_y)
            {
                return -1;
            }
            else
                return 1;
        }
        return 0;
    }

采纳答案by Thomas Levesque

This should do what you want:

这应该做你想做的:

var example = new string[]{"c", "a", "d", "b"};
var comparer = new CustomStringComparer(StringComparer.CurrentCulture);
Array.Sort(example, comparer);

...

class CustomStringComparer : IComparer<string>
{
    private readonly IComparer<string> _baseComparer;
    public CustomStringComparer(IComparer<string> baseComparer)
    {
        _baseComparer = baseComparer;
    }

    public int Compare(string x, string y)
    {
        if (_baseComparer.Compare(x, y) == 0)
            return 0;

        // "b" comes before everything else
        if (_baseComparer.Compare(x, "b") == 0)
            return -1;
        if (_baseComparer.Compare(y, "b") == 0)
            return 1;

        // "c" comes next
        if (_baseComparer.Compare(x, "c") == 0)
            return -1;
        if (_baseComparer.Compare(y, "c") == 0)
            return 1;

        return _baseComparer.Compare(x, y);
    }
}

回答by Igby Largeman

A simple way is to substitute integers for the strings.

一个简单的方法是用整数代替字符串。

class MyComparer : IComparer<string>
{
    public override int Compare(string x, string y)
    {
        int ix = x == "b" ? 0 : x == "c" ? 1 : 2;
        int iy = y == "b" ? 0 : y == "c" ? 1 : 2;
        return ix.CompareTo(iy);
    }
}

var example = new List<string> { "c", "b", "a", "d", "foo", "", "1", "e"};
example.Sort(new MyComparer());
foreach (var s in example)
    Console.WriteLine(s);

Output:

b
c

1
e
a
d
foo

输出:


1
e
a
d
foo

Note that this isn't a stable sort. If you need a stable sort, there's a little more work involved.

请注意,这不是一种稳定的排序。如果您需要稳定的排序,则需要做更多的工作。