I'm new to lambda expressions and just ran into something I don't understand.
I have an object like this:
class MyListItem { string date;
I have a list of these objects representing some dates and hours.
I want to sort this list by date and hour, so I try this:
List<MyListItem> myList = new List<MyListItem>(); myList = getsomedata(); //populate list myList.Sort((a, b) => (a.date + a.Hour.ToString()).CompareTo(b.date + b.Hour.ToString()));
and it works, sort of. The problems are that the hour is int, so sometimes it is not 2 digits, which leads to similar types:
2010-12-05 1 2010-12-05 10 2010-12-05 11 2010-12-05 12 2010-12-05 13 2010-12-05 2 2010-12-05 21 2010-12-05 22
I want it to be like this:
2010-12-05 1 2010-12-05 2 2010-12-05 10 2010-12-05 11 2010-12-05 12 2010-12-05 13 2010-12-05 21 2010-12-05 22
so I'm trying to format the string to add zero before parsing it together in lambda:
ret.Sort((a, b) => (a.date + a.Hour.ToString("00")).CompareTo(b.date + b.Hour.ToString("00")));
But it will not compile. This tells me:
Cannot convert lambda expression to type 'Systems.Collections.Generic.IComparer<MyListItem>' because it is not a delegate type.
A? What is the difference between regular .ToString () (without a format string) and .ToString ("00") in this situation?
Also, any suggestions on how to do this?