c# sort listbox

Discussion in 'Software' started by mastermosley, Nov 19, 2010.

  1. mastermosley

    mastermosley Sergeant

    I need some sort of function to sort the listbox based on the date. As an example the listbox has:

    [Wednesday, November 24, 2010 5:30PM] - Example Text
    [Friday, December 24, 2010 1:30 PM] - Example Text
    [Thursday, November 25, 2010 12:00PM] - Example Text

    I have no idea how I would do this any ideas?
     
  2. PC-XT

    PC-XT Master Sergeant

    Override the sort method to return DateTime.Parse(x).CompareTo(DateTime.Parse(y))

    Actually, this sort method actually does the sort...
    I'll think about this some more...
     
    Last edited: Nov 20, 2010
  3. PC-XT

    PC-XT Master Sergeant

    http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.sort.aspx gives an example of overriding the method. The algorithm it uses is inefficent, but if you have few items to sort, it hopefully won't be noticeable. You will need to change it from sorting by Items(counter[+offset]).ToString.Length to sorting by DateTime.Parse(Items(counter[+offset]).ToString)

    It seems like there should be a way you can just sort Items, using a function like the one I gave first... but if this works, it works.
     
    Last edited: Nov 20, 2010
  4. Wyatt_Earp

    Wyatt_Earp MajorGeek

    IMO, the easiest way might be to do something like:

    Code:
    string dateFormat = "dddd, MMMM dd, yyyy H:MMtt";
    List<DateTime> Dates;
    List<string> dateStrings;
    
    foreach(string s in ListBox1.Items)
        Dates.add(DateTime.Parse(s, dateFormat));
    
    Dates.Sort();
    
    dateStrings = new List<string>();
    
    foreach(DateTime d in Dates)
        dateStrings.Add(d.ToString(dateFormat));
    
    ListBox1.Items.AddRange(dateStrings);
    
    I think you could do this with fewer lines using lambda expressions, but I don't have Visual Studio on this computer so I can't test it out. Also, depending on what structure you are populating the list box from, you could skip the first half of the code and just sort it before you populate it.
     
  5. PC-XT

    PC-XT Master Sergeant

    Yes, Wyatt_Earp's way is best if you can just add them all at once, that is, if you don't need to change the ListBox choices afterward. If you do need to change them, you can still use the same method. Just change the first List and resort it, then repopulate the second List and ListBox.

    It does take more memory to keep 2 lists, but it doesn't matter so much, since the first is just DateTimes, rather than strings.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds