I was determined to find a way to sort an array or list of objects easily. Of course most people are familiar with the standard “.sort” method that you pass a comparison object that inherits from IComparable as follows:
This seems so tedious to have to create a custom sort class for a one time sort operation (probably a bit of laziness too…). Thats when I came across some nice Linq extensions code that is in the .NET Framework 3.0+. It definitely makes things quite a bit easier. Take a look at this example showing how to easily sort files by date:
The order by method accepts a Linq expression, f representing the object we are referring too. Even works with the objects intellisense. Well done Microsoft. Alternatively, you can sort decescending using the OrderByDescending method exactly the same: