Skip to content

Instantly share code, notes, and snippets.

@creyke
Created March 26, 2021 09:39
Show Gist options
  • Save creyke/8dd6f2a39f26c94282181230ae05a835 to your computer and use it in GitHub Desktop.
Save creyke/8dd6f2a39f26c94282181230ae05a835 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace MarketSort.Solutions
{
class SorterD : ISorter
{
public IEnumerable<string> Sort(IEnumerable<string> values)
{
var converted = values.Select(x => ConvertToHex(x)).OrderBy(x => x).OrderByDescending(x => x.Length);
return converted.Select(x => ConvertBack(x));
}
private static string ConvertToHex(string score)
{
if (score.Length > 7)
{
return score;
}
var scores = score.Split("-");
var home = Int32.Parse(scores[0]);
var away = Int32.Parse(scores[1]);
var result = $"{home:X}-{away:X}";
return result;
}
private static string ConvertBack(string score)
{
if (score.Length > 7)
{
return score;
}
var scores = score.Split("-");
var home = Convert.ToInt32(scores[0], 16);
var away = Convert.ToInt32(scores[1], 16);
return $"{home}-{away}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment