using System.Diagnostics;
using System.Collections.Generic;
public string Name { get; set; }
public string BlockName { get; set; }
public string PreviousBlockName { get; set; }
public override string ToString() {
return $"Name:{Name} BlockName:{BlockName} PreviousBlockName:{PreviousBlockName}";
public static void Main()
var howMuchSampleDataToGenerate = 1000;
var unsorted = GetSampleData(howMuchSampleDataToGenerate);
var sw = new Stopwatch();
var blockNames = unsorted.Select(x => x.BlockName).ToHashSet();
var first = unsorted.First(x => !blockNames.Contains(x.PreviousBlockName));
var dictionary = unsorted
.Where(x => x.BlockName != first.BlockName)
.ToDictionary(block => block.PreviousBlockName);
var sorted = new Block[unsorted.Count];
for (var i = 0; i < dictionary.Count; i++)
var next = dictionary[current.BlockName];
Console.WriteLine($"Total time to sort in milliseconds: {sw.ElapsedMilliseconds}");
public static List<Block> GetSampleData(int amount)
var unsorted = Enumerable.Range(0, amount)
BlockName = CreateMd5(x),
PreviousBlockName = CreateMd5(x - 1)
public static string CreateMd5(int x)
var input = x.ToString();
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
sb.Append(hashBytes[i].ToString("X2"));