Skip to content

Instantly share code, notes, and snippets.

@Suchiman
Forked from brianmed/Program.cs
Created June 2, 2019 22:51
Show Gist options
  • Save Suchiman/c78b8c7bf5aac0326472c848a13135c2 to your computer and use it in GitHub Desktop.
Save Suchiman/c78b8c7bf5aac0326472c848a13135c2 to your computer and use it in GitHub Desktop.
LexicographSort Integer List
using System;
using System.Collections.Generic;
using System.Linq;
namespace LexicographicSortIntegerList
{
class Program
{
static void Main(string[] args)
{
while (true)
{
// echo "41 4\n" | dotnet run (Output 441)
List<int> digits = "41 4"
.Split(' ')
.Select(Int32.Parse)
.ToList();
digits.Sort((a, b) =>
{
int aMagnitude = (int)Math.Pow(10, (int)(Math.Log10(a) + 1));
int bMagnitude = (int)Math.Pow(10, (int)(Math.Log10(b) + 1));
int left = a * bMagnitude + b;
int right = b * aMagnitude + a;
return right.CompareTo(left);
});
Console.WriteLine(String.Join("", digits));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment