Custom Sorting of List<T>
c#, generics, lambda, sorting
Solution
Try
events.OrderBy (e => e.Time == 0).ThenBy (e => e.Time);
Problem
I have a `List<T>` where `T` is my `Event` type which has a field `time` of type `long`. This list is populated from a web service and if an event doesn't have a time, the value set is 0. What i want to do is sort my list ascending by time, but place the items with time=0 at the very bottom. Currently I am accomplishing this in a hack sort of fashion and I want to learn a better way. ``` var events = new ObservableCollection<Event>(); var resp = JsonConvert.DeserializeObject<Events>(restResponse.Content).Items; var notime = resp.Where(r => r.time == 0); var yestime = resp.Where(r => r.time > 0); yestime.ToList().ForEach(events.Add); notime.ToList().ForEach(events.Add); CallbackInternal(callback, events); ``` I attempted implementing a custom `IComparer`, but that didn't work out so well (here is one shot at it) ``` public class EventComparer : IComparer<Event> { public int Compare(Event x, Event y) { if (x.time == 0) return 0; if (x.time < y.time) return -1; if (x.time > y.time) return 1; return 0; } } ``` guidance is appreciated! thanks!