Created
May 4, 2014 22:43
-
-
Save cullen-tsering/a02ecdf3ba09a2f23782 to your computer and use it in GitHub Desktop.
An architectural decision and simple custom sorting
This file contains 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
class Program | |
{ | |
static void Main() | |
{ | |
// Read in every line in the file. | |
var list = new List<JobSchedule>{ | |
new JobSchedule{ | |
Customer = "jon", | |
SchedulePriority = new SchedulePriority("P","PM") | |
}, | |
new JobSchedule{ | |
Customer = "jane", | |
SchedulePriority = new SchedulePriority("O","Any") | |
}, | |
new JobSchedule{ | |
Customer = "bob", | |
SchedulePriority = new SchedulePriority("A","AM") | |
}, | |
new JobSchedule{ | |
Customer = "bonnie", | |
SchedulePriority = new SchedulePriority("F","First") | |
}, | |
}; | |
Console.WriteLine("Sort by Alphabetical Order and the result does NOT match the requirement"); | |
foreach (var js in list.OrderBy(m => m.SchedulePriority.Code)) | |
{ | |
Console.WriteLine(js.ToString()); | |
} | |
Console.WriteLine("Sort by custom order and the result MATCH the requirement"); | |
foreach (var js in list.OrderBy(m => m.SchedulePriority, new SchedulePriorityOrderComparer())) | |
{ | |
Console.WriteLine(js.ToString()); | |
} | |
} | |
} | |
public class JobSchedule | |
{ | |
public string Customer { get; set; } | |
public SchedulePriority SchedulePriority { get; set; } | |
public override string ToString() | |
{ | |
return string.Format("{0} :: {1}", Customer, SchedulePriority); | |
} | |
} | |
public class SchedulePriority | |
{ | |
public SchedulePriority(string code, string description) | |
{ | |
Code = code; | |
Description = description; | |
} | |
public string Code { get; private set; } | |
public string Description { get; private set; } | |
public override string ToString() | |
{ | |
return string.Format("{0} :: {1}", Code, Description); | |
} | |
} | |
public class SchedulePriorityOrderComparer : IComparer<SchedulePriority> | |
{ | |
readonly Dictionary<string, int> _priorities = new Dictionary<string, int>() { | |
{"F", 1}, | |
{"A", 2}, | |
{"O", 3}, | |
{"P", 4} | |
}; | |
public int Compare(SchedulePriority x, SchedulePriority y) | |
{ | |
if (string.IsNullOrWhiteSpace(x.Code) || string.IsNullOrWhiteSpace(y.Code)) return 0; | |
return _priorities[x.Code] - _priorities[y.Code]; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment