我想了解 IComparer 在下面的程序中是如何工作的

I want to understand how the IComparer works in the below program

给定一个数字数组,以产生最大值的方式排列它们。 例子 {54,546,548,60} 排列是6054854654给出最大值。

class IntegerConcatComparer : IComparer<int>
{
    public int Compare(int x, int y)
    {

        var xy = int.Parse(x.ToString() + y.ToString());
        var yx = int.Parse(y.ToString() + x.ToString());
        return xy - yx;
    }
}

public class Program
{
    static void Main(string[] args)
    {
        var InputArrays = new int[] { 65, 546, 548, 54 };
        var Output = PossibleSequence(InputArrays);
        Console.WriteLine("\n{0} ", Output);

    }


    static string PossibleSequence(int[] ints) 
    {
        var res= ints.OrderBy(sap => sap, new IntegerConcatComparer());
        var reveresedOrder= res.Reverse().ToArray();
       return string.Join(" ", reveresedOrder);
    }

}

输出将为 60 548 546 54。 我需要了解 IComparer 如何排序此数组

提前致谢

您的代码的重点是尝试通过串联整数列表来找到最大可能的数字。如果你将该逻辑分解为两个数字,比如前两个 ([65,546]) 比较器将它们以两种方式连接起来以确定哪个数字更大,它会问:

"Is 65546 bigger or smaller than 54665".

然后它继续对数组中的所有其他数字组合执行此操作,以这样的方式对它们进行排序,即当连接在一起时,使最大的数字成为可能。

我写了some code that might help you understand,

本质上,比较器可以帮助您按 最高有效数字 排序,您可以使用我在 PowComp 中提供的实现来避免字符串操作。我怀疑使用一些二进制数学可能有更有效的方法来做到这一点。

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var concatComp = new ConcatComp();
        var modComp = new ModComp();
        var powComp = new PowComp();

        var tests = new[]
            {
                Tuple.Create(0, 0),
                Tuple.Create(1, 9),
                Tuple.Create(9, 1),
                Tuple.Create(1, 999),
                Tuple.Create(999, 1),
                Tuple.Create(111, 9),
                Tuple.Create(9, 111),
                Tuple.Create(91, 19),
                Tuple.Create(19, 91)
            };

        foreach(var pair in tests)
        {
            var concatR = R(concatComp.Compare(pair.Item1, pair.Item2));
            var modR = R(modComp.Compare(pair.Item1, pair.Item2));
            var powR = R(powComp.Compare(pair.Item1, pair.Item2));
            Console.WriteLine(
       $"x:{pair.Item1}, y:{pair.Item2}, concatR:{concatR}, modR:{modR}, powR:{powR}");
        }
    }


    public static string R(int v)
    {
        if (v == 0)
            return "=";

        if (v < 0)
            return "y";

        return "x";
    }
}

public class ConcatComp : IComparer<int>
{
    public int Compare(int x, int y)
    {

        var xy = int.Parse(x.ToString() + y.ToString());
        var yx = int.Parse(y.ToString() + x.ToString());
        return xy - yx;
    }
}

public class ModComp : IComparer<int>
{
    public int Compare(int x, int y)
    {

        return (x % 10) - (y % 10);
    }
}

public class PowComp : IComparer<int>
{
    public int Compare(int x, int y)
    {

        return MSD(x) - MSD(y);
    }

    public int MSD(int v)
    {
        if (v < 10)
            return v;

        return v / (int)Math.Pow(10.0, (int)Math.Log10(v));
    }
}

代码输出这些结果。

x:0, y:0, concatR:=, modR:=, powR:=
x:1, y:9, concatR:y, modR:y, powR:y
x:9, y:1, concatR:x, modR:x, powR:x
x:1, y:999, concatR:y, modR:y, powR:y
x:999, y:1, concatR:x, modR:x, powR:x
x:111, y:9, concatR:y, modR:y, powR:y
x:9, y:111, concatR:x, modR:x, powR:x
x:91, y:19, concatR:x, modR:y, powR:x
x:19, y:91, concatR:y, modR:x, powR:y