To simplify my question, I’ll will give an example.
Say I have a list of objects containing a datetime and an integer attribute.
I’m ordering my list items first by the datetime field then by the integer attribute.
Item # Date Value Int Value
1 2012-01-01 0
2 2012-01-01 4
3 2012-01-01 6
4 2012-01-01 13
Say I have those 4 items in my list (un-ordered at first, above is how I want it to be in the end).
I’m ordering them simply by:
myList = myList.OrderBy(v=>v.MyDateValue).ThenBy(v=>v.MyIntValue).ToList();
After executing the line above, it ends up like below :
Item # Date Value Int Value
1 2012-01-01 0
4 2012-01-01 13
2 2012-01-01 4
3 2012-01-01 6
At first I thought it interprets my int value as a string perhaps (probably not a logical statement, just been futzing with this for quite some time) then I changed my int values to 10,11,12,13 but the result was still the same.
Any idea why this doesn’t work ? Or what I’m doing wrong?
Solution: change your key selector to
v => v.DateValue.Date, if you’re not interested in the time.I couldn’t duplicate the behavior you described. The following works as expected:
Output item order is 1, 4, 12, and 7. (7 is last because its date is later.)
I suspect that you have
DateTimevalues whoseTimeis not 0.Verify that by changing your key selector to
v => v.DateValue.Date.