Last active
March 1, 2018 11:09
-
-
Save gsedubun/3c8192095d26f8c093510fe813aa3de0 to your computer and use it in GitHub Desktop.
Demo implementation of IComparable<T>
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
class Program | |
{ | |
private static WorkDay workday1 = new WorkDay { Day = 1, DayName = "Monday" }; | |
private static WorkDay workday2 = new WorkDay { Day = 2, DayName = "Tuesday" }; | |
private static WorkDay workday3 = new WorkDay { Day = 3, DayName = "Wednesday" }; | |
private static WorkDay workday4 = new WorkDay { Day = 4, DayName = "Thursday" }; | |
private static WorkDay workday5 = new WorkDay { Day = 5, DayName = "Friday" }; | |
private static Shift shift1 = new Shift { Start = TimeSpan.Parse("08:00"), End = TimeSpan.Parse("12:00"), Name = "Shift 1" }; | |
private static Shift shift2 = new Shift { Start = TimeSpan.Parse("12:00"), End = TimeSpan.Parse("16:00"), Name = "Shift 2" }; | |
private static Shift shift3 = new Shift { Start = TimeSpan.Parse("16:00"), End = TimeSpan.Parse("20:00"), Name = "Shift 3" }; | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
List<Schedule> list = new List<Schedule> | |
{ | |
new Schedule | |
{ | |
Shift = shift1, | |
WorkDay= workday1 | |
}, | |
new Schedule | |
{ | |
Shift = shift2, | |
WorkDay= workday4 | |
}, | |
new Schedule | |
{ | |
Shift = shift1, | |
WorkDay= workday2 | |
}, | |
new Schedule | |
{ | |
Shift = shift2, | |
WorkDay= workday1 | |
}, | |
new Schedule | |
{ | |
Shift = shift1, | |
WorkDay= workday5 | |
}, | |
new Schedule | |
{ | |
Shift = shift2, | |
WorkDay= workday3 | |
}, | |
}; | |
Console.WriteLine("Before Sorting"); | |
foreach (var item in list) | |
{ | |
Console.WriteLine($"{item.WorkDay.DayName} - on {item.Shift.Name} from {item.Shift.Start} to {item.Shift.End}."); | |
} | |
Console.WriteLine("\n\n\n\n"); | |
list.Sort(); | |
Console.WriteLine("After Sorting"); | |
foreach (var item in list) | |
{ | |
Console.WriteLine($"{item.WorkDay.DayName} - on {item.Shift.Name} from {item.Shift.Start} to {item.Shift.End}."); | |
} | |
Console.ReadKey(); | |
} | |
} |
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
class Schedule : IComparable<Schedule> | |
{ | |
public int CompareTo(Schedule other) | |
{ | |
int result = 0; | |
result += WorkDay.CompareTo(other.WorkDay); | |
result += Shift.CompareTo(other.Shift); | |
return result; | |
} | |
public WorkDay WorkDay { get; set; } | |
public Shift Shift { get; set; } | |
} |
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
public struct Shift : IComparable<Shift> | |
{ | |
public string Name { get; set; } | |
public TimeSpan Start { get; set; } | |
public TimeSpan End { get; set; } | |
public int CompareTo(Shift other) | |
{ | |
if (Start > other.Start) | |
return 1; | |
else if(Start< other.Start) | |
return -1; | |
return 0; | |
} | |
} |
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
public struct WorkDay : IComparable<WorkDay> | |
{ | |
public int Day { get; set; } | |
public string DayName { get; set; } | |
public int CompareTo(WorkDay other) | |
{ | |
if (Day > other.Day) | |
return 2; | |
else if(Day<other.Day) | |
return -2; | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment