Reputation: 23187
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
Upvotes: 3
Views: 8219
Reputation: 3873
If your String contains anything other then a date, then I highly recommend splitting your string into JUST the date part, then TryParse and compare that value. Not the whole string!
yourStringList.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);
}
);
In this example, im splitting my string to exactly the part where the date lies, and using tryparse on that. Only after that is parsed correctly can your comparing be done properly.
Upvotes: 0
Reputation: 754753
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.OrderBy(x => DateTime.Parse(x[1]));
Upvotes: 7
Reputation: 195
Maybe something like
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);
});
Upvotes: 0
Reputation: 2173
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.
Upvotes: 0
Reputation: 1507
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);
}
}
Upvotes: 2
Reputation: 131676
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()
.
Upvotes: 0