For references sake, I can use .Sort() and .Reverse(), but the first object in the list is a DateTime.ToString() + "other string stuff here"...
My first attempt was literally 3-4 loops with new lists and comparing converted DateTime's while trying to modify the original list with the new list. Suffice to say, it was a nightmare and I didn't get it to work.
So, I shrunk it down, parsing out the datetime string and adding leading 0's if it needed them. Converting that string to a DateTime using ParseExact got rid of leading 0's, but still didn't sort correctly. I then tried keeping the new leading 0's datetime strings and sorting that, and while working, it didn't sort the year part correctly. So, here I am...
If I parse out the strings to DateTime's, I need to sort the list on that, but the sorted list needs to append the original strings that the items[index] had with the date.
I think it's harder to explain on here, but the sort as it is now, doesn't sort the items the way I imagined...
Ex) the list contains:
12/22/2011 11:00:00 PM + string
12/22/2012 10:00:00 PM + string
12/23/2010 09:00:00 PM + string
it sorts to:
12/23/2010 09:00:00 PM + string
12/22/2012 10:00:00 PM + string
12/22/2011 11:00:00 PM + string
when I am looking for:
12/22/2012 10:00:00 PM + string
12/22/2011 11:00:00 PM + string
12/23/2010 09:00:00 PM + string
Here's my last attempt:
Code:public void reversesort() { for (int i = 0; i < items.Count; i++) { string itemdatestr = ""; string itemtempstr = items[i].text; for (int l = 0; l < 3; l++) { itemdatestr += itemtempstr.Substring(0, itemtempstr.IndexOf(" ") + 1); itemtempstr = itemtempstr.Remove(0, itemtempstr.IndexOf(" ") + 1); } string newfrom = " " + itemtempstr; itemdatestr = itemdatestr.Substring(0, itemdatestr.Length - 1); if (itemdatestr[1].ToString() == "/") itemdatestr = "0" + itemdatestr; string itemdatedaystr = itemdatestr.Remove(0, 3); if (itemdatedaystr[1].ToString() == "/") itemdatedaystr = "0" + itemdatedaystr; itemdatestr = itemdatestr.Substring(0, 3) + itemdatedaystr; //DateTime itemdate = DateTime.ParseExact(itemdatestr, "MM/dd/yyyy h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture); items[i].text = itemdatestr + newfrom; } items.Sort(); items.Reverse(); }


Reply With Quote


