I wrote some code that might help you understand,
Essentially, the comparer helps you order by the most significant digit, you can avoid the string manipulation using the implementation I provided in PowComp
. I suspect there is probably an even more efficient way to do this using some binary maths.
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));
}
}
The code outputs these results.
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