-
Mar 15th, 2012, 09:33 PM
#1
Thread Starter
Hyperactive Member
List sorting, but doesn't just use 1 data object?
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();
}
-
Mar 15th, 2012, 10:33 PM
#2
Re: List sorting, but doesn't just use 1 data object?
Follow the Blog link in my signature. The oldest three posts are on sorting arrays and collections. That will give you everything you need. You'll understand this when you've read them: when using the IComparer interface or the Comparison delegate, the function that compares two items can be as simple or as complex as you like.
-
Mar 18th, 2012, 06:55 PM
#3
Re: List sorting, but doesn't just use 1 data object?
Originally Posted by jmcilhinney
...IComparer interface or the Comparison delegate, the function that compares...
I think you meant to say the IComparable interface. I recently tested this for an answer for another thread and I had to use that interface for it to work. I think IComparer is for the objects that do the sorting, ie. the collections and lists.
-
Mar 18th, 2012, 08:18 PM
#4
Re: List sorting, but doesn't just use 1 data object?
Originally Posted by Niya
I think you meant to say the IComparable interface. I recently tested this for an answer for another thread and I had to use that interface for it to work. I think IComparer is for the objects that do the sorting, ie. the collections and lists.
No, I meant IComparer. If you call Sort with no arguments then it relies on the IComparable implementation of the objects themselves. If the items don't implement IComparable or you want to sort in a different way then you can pass an IComparer object or a Comparison delegate. All three (IComparable, IComparer and Comparison) do exactly the same thing though: they provide a method to compare two objects and return a value representing their relative order. The entity actually doing the sorting, i.e. the Array class or the collection object, then invokes that method over and over to order all the items relative to each other based on those values.
-
Mar 18th, 2012, 09:47 PM
#5
Re: List sorting, but doesn't just use 1 data object?
I think the underlying issue is that you're working with a List<string> or array of strings perhaps?). What you should be working with is a List (or array) of some custom type, possibly something called TimestampedString?
You don't have a string, you have an object that has a string property and a DateTime property.
You could then have this implement IComparable if it makes sense to "default" to sorting by the timestamp. Or if you write an IComparer, you will find it simpler to not have to munge about with parsing the values out of the conjoined string.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|