List of Sort Lists with String Dates?

I have a list of string lists containing DateTime values ​​converted to strings. There are other values ​​in the list, so I cannot make the list a complete DateTime list.

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

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

edit:

The contents of the list of samples:
Value1, 2010-06-28 10: 30: 00.000
Value2, 2010-06-27 10: 30: 00.000
Value2, 2010-06-26 10: 30: 00.000

+3
source share
6 answers

Try the following

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

, LINQ inplace

var sorted = myList.OrderBy(x => DateTime.Parse(x[1]));
+7

.

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);
    }
}
+2

DateTime. , /. Linq :

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

, , :

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

, , DateTime.ParseExact().

0

[1] .

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

EDIT:

LINQ, , , , . DateTime.Parse , .

, string.Split, , , , .

0

-

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((a, b) =>
    {
        string[] aSplit = a.Split(',');
        string[] bSplit = b.Split(',');

        if (aSplit.Count() < 2 && bSplit.Count() < 2) 
            return a.CompareTo(b);

        DateTime date1, date2;

        if (!DateTime.TryParse(aSplit[1].Trim(), out date1) || 
            !DateTime.TryParse(bSplit[1].Trim(), out date2))
            return a.CompareTo(b);

        return date2.CompareTo(date1);
    });
0

-, , , TryParse . !

requirementDays.Sort(
                (x, y) => {
                    DateTime ix;
                    DateTime iy;
                    DateTime.TryParse(x.Split('(')[2].Split(')')[0], out ix);
                    DateTime.TryParse(y.Split('(')[2].Split(')')[0], out iy);

                    return ix.CompareTo(iy);
                }
            );

, , . , , .

0

Source: https://habr.com/ru/post/1752103/


All Articles