CustomSort.cs

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
					
public class Program
{
	public class CustomSort: IComparer<string>
	{
		// Кастомная сортировка по след.правилу: 
		// строка X меньше строки Y тогда и только тогда, 
		// когда строка X+Y меньше строки Y+X.
		public int Compare(string x, string y)
		{
			return (y + x).CompareTo(x + y);
		}
	}
	
	static string GetMaxNumberFromStrings(string[] numbers)
	{
		// сортируем массив по нужному правилу: CustomSort
		Array.Sort(numbers, new CustomSort());
		
		// "Склеиваем" отсортированный массив и получаем результат
		string result = string.Join("", numbers);
		return result;
	}	
	
	public static void Main()
	{
		Console.WriteLine("UniLecs");
		
		string[] arr1 = new string[] { "123", "124", "56", "90" };
		Console.WriteLine(string.Format("Answer = {0}", GetMaxNumberFromStrings(arr1))); // "9056124123"
		
		string[] arr2 = new string[] { "123", "124", "56", "90", "9" };
		Console.WriteLine(string.Format("Answer = {0}", GetMaxNumberFromStrings(arr2))); // "99056124123"
		
		string[] arr3 = new string[] { "123", "127", "1239" };
		Console.WriteLine(string.Format("Answer = {0}", GetMaxNumberFromStrings(arr3))); // "1271239123"
	}
}
;