tags:

views:

63

answers:

4

I have a list of string lists that contains DateTime values converted to strings. There are other values in the list, so I can't make the list a full DateTime list.

I have a line of code that sorts the list, but it sorts the dates by their string value, not DateTime value (which is what I want). How can I modify my code to correctly sort by the DateTime?

//This sorts the parent list by the 2nd column of the child list
List.Sort((a, b) => -1 * a[1].CompareTo(b[1]));

edit:

Sample List Contents:
Value1, 2010-06-28 10:30:00.000
Value2, 2010-06-27 10:30:00.000
Value2, 2010-06-26 10:30:00.000

+2  A: 

Try the folloting

List.Sort((a,b) => -1 * DateTime.Parse(a[1]).CompareTo(DateTime.Parse(b[1])));

Or if you have LINQ handy and don't need an inplace sort

var sorted = myList.Sort(x => DateTime.Parse(x[1]));
JaredPar
DateTime.Parse throws the following error: String was not recognized as a valid DateTime.Do I need to reconvert the DateTime to a string?
Soo
@Soo then the strings in your collection are not actually date times as your question specified (or they are in a unrecognized format). How did these strings get into this data structure? Can you show us some code?
JaredPar
@JaredPar,The data format is coming from a LINQ query that grabs results from an SQL database. Shouldn't C# be able to recognize the date format anyway? I'm confused ...
Soo
A: 

You first parse the string into a DateTime. Assuming the second columns is always a well-formed date/time string. The code below uses Linq to create a separate sorted list:

sortedList = yourList.OrderBy( item => DateTime.Parse( item[1] ) ).ToList();

If you need to sort in place, you can do so using:

List.Sort((a,b)=>return DateTime.Parse(b[1]).CompareTo(DateTime.Parse(a[1]) );

Depending on how your dates are repsented, you may want to consider using DateTime.ParseExact().

LBushkin
Considering how my dates are formatted:yyyy-mm-dd hh:mm:ss.mmmHow would I use ParseExact() to make this work properly?
Soo
A: 

You need to convert a[1] to date time before comparing it.

List.Sort((a, b) => -1 * DateTime.Parse(a[1]).CompareTo(DateTime.Parse(b[1])))

EDIT:

If your LINQ query that fills the list is filling it as a list of strings, then you need to get a substring of the item that represents the date portion of the string. DateTime.Parse won't just magically pick out the part of the string that has a date value in it.

If this is the case and you're considering using string.Split, be sure you know the format your date will be coming, since some formats allow commas.

Sam T.
Let me see if I understand this correctly. a[1] and b[1] represent the second value in each child list, which should be DateTime items themselves. Shouldn't DateTime.Parse in that case work (it isn't). I'm not sure why DateTime.Parse isn't working because (from my understanding) a[1] and b[1] are already date strings.
Soo
+1  A: 

You should create your own custom comparer.

class Program
{
    static void Main(string[] args)
    {
        List<string> list = new List<string>
                                {
                                    "Value1, 2010-06-28 10:30:00.000",
                                    "Value2, 2010-06-27 10:30:00.000",
                                    "Value3, 2010-06-26 10:30:00.000"
                                };

        list.Sort(new MyComparer());
    }
}

internal class MyComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        var xItems = x.Split(new []{','});
        var yItems = y.Split(new []{','});
        var xDateTime = DateTime.Parse(xItems[1]);
        var yDateTime = DateTime.Parse(yItems[1]);
        return xDateTime.CompareTo(yDateTime);
    }
}
Nitin Chaudhari