Sorting a list based on date and time I have a class that is used to basically store samples from data being read from a system. Within that class is a property that I use to store the date and time of that sample:

public class ReadingValue
{
    public DateTime DateTimeOfReading { get; set; }
    public float Reading { get; set; }
}


I need to sort a list of ReadingValue based on the DateTimeOfReading property.

This is what I've come up with:

public IEnumerable&lt;ReadingValue&gt; Hourly(IList&lt;ReadingValue&gt; readings)
{
    var sortedReadings = readings.OrderBy(x =&gt; x.DateTimeOfReading.TimeOfDay)
        .OrderBy(x =&gt; x.DateTimeOfReading.Date)
        .OrderBy(x =&gt; x.DateTimeOfReading.Year);

    return sortedReadings;
}


The code seems to work, when I pass in a list of ReadingValue's it returns the list in sorted order. Is there anything I've overlooked or should do differently? I know time and date programming can be a tricky topic with things like timezones, daylight savings etc. but sorting dates and times shouldn't involve any of that, I don't think.
You should first do OrderBy and then you should do ThenBy instead of doing another OrderBy. What you are currently doing is that you are re-ordering everything after your first OrderBy.

public IEnumerable&lt;ReadingValue&gt; Hourly(IList&lt;ReadingValue&gt; readings)
{
    var sortedReadings = readings.OrderBy(x =&gt; x.DateTimeOfReading.TimeOfDay)
        .ThenBy(x =&gt; x.DateTimeOfReading.Date)
        .ThenBy(x =&gt; x.DateTimeOfReading.Year);

    return sortedReadings;
}