I have a generic list which I form from two other lists. The orderby doesn’t seem to be working correctly. BeOrders come first (ordered by date) then BTOrders (ordered by date). Have I missed something obvious? I can’t see anything wrong.
orders = new List<DataLayer.OrderStatusItem>();
orders.AddRange(BeOrders);
orders.AddRange(BTOrders);
orders.OrderBy(z => z.ordered);
Yes, you’ve missed that
OrderBydoesn’t order in-place – it returns an ordered sequence:Or (assuming that
ordersis of typeList<...>:Note that this “no side effects” approach is prevalent throughout LINQ – none of the operators change the collection they’re called on; they return a view onto that collection of some form (filtered, projected etc).
If you want to sort a
List<T>in place you can useList<T>.Sort. (You’ll have to specify the ordering in a different way though.)