Results 1 to 5 of 5

Thread: List sorting, but doesn't just use 1 data object?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    PA
    Posts
    365

    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();
            }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: List sorting, but doesn't just use 1 data object?

    Quote Originally Posted by jmcilhinney View Post
    ...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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: List sorting, but doesn't just use 1 data object?

    Quote Originally Posted by Niya View Post
    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.

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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
  •  



Click Here to Expand Forum to Full Width