Created
March 26, 2021 09:35
-
-
Save creyke/046d835e5d9e4a6e475371139ef65d6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
namespace MarketSort.Solutions | |
{ | |
class SorterB : ISorter | |
{ | |
public IEnumerable<string> Sort(IEnumerable<string> values) | |
{ | |
var ordered = new List<string>(); | |
var processed = values | |
.Select(v => (splitted: v.Split("-"), key: v)) | |
.OrderBy(v => { | |
if (v.splitted.Length <= 1 || v.key.Any(char.IsLetter)) return -1; | |
int.TryParse(v.splitted[1], out var converted); | |
return converted; | |
}) | |
.OrderBy(v => { | |
if (v.splitted.Length <= 1 || v.key.Any(char.IsLetter)) return -1; | |
int.TryParse(v.splitted[0], out var converted); | |
return converted; | |
}); | |
foreach (var p in processed) | |
{ | |
ordered.Add(p.key); | |
} | |
return ordered; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment