I have a complicated sort/grouping that needs to be performed with a list of objects.
The objects have three properties: EventName Start End
I'm given the name of an event "EventName" and the start/end times for said event, and populate a List.
Now comes the hard part.
The requirements are: - the same EventName can have multiple entries with different start/end times - events are to be sorted by start time, with the earliest occurring first - events, if they have the same start time, should then be sorted by shortest to longest run times. - events are then to be grouped, so all events with the same name are listed in ascending order, under the event name. - Events with different names, but the same overall earliest start time are to be sorted alphabetically.
For example:
List(0):
EventName: Test 2 Start: 10am End: 3pm
List(1):
EventName: Test 2 Start: 10am End: 11am
List(2):
EventName: Test 1 Start: 12pm End: 1pm
List(3):
EventName: Test 2 Start: 2pm End: 3pm
So when all is said and done, the List needs to be sorted to be:
List(0):
EventName: Test 2 Start: 10am End: 11am
List(1):
EventName: Test 2 Start: 10am End: 3pm
List(2):
EventName: Test 2 Start: 2pm End: 3pm
List(3):
EventName: Test 1 Start: 12pm End: 1pm
I'm really not sure how to do this. I have this much which doesn't work:
theList.OrderBy(o => o.Start).ThenBy(o => o.End).ThenBy(o => o.EventName);
Any help would be appreciated.
EDIT To make it easier, assume times are converted in 24-hour time and stored as ints as I can easily do that.
I believe I have it!
Just had to do some minor tweaks to what I already had.
Here's the code that thus far is working as I need it:
theList.GroupBy(o => o.EventName).Select(s => s.OrderBy(a => a.Start).ThenBy(a => a.End)).SelectMany(sm => sm).ToList();
Where all the other suggestions were falling down was keeping the EventNames grouped together when sorting by the start times.
_