Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Sorting a Listbox Descending

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Resolved [RESOLVED] [2005] Sorting a Listbox Descending

    Hi Guys

    In VB 2005 I have a list box which is getting populated during the runtime. How do I show the listbox in descending sort?

    Newbie thankx.

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

    Re: [2005] Sorting a Listbox Descending

    A standard ListBox will always sort in ascending order. You have two choices:

    1. Define your own class that inherits ListBox and override the Sort method to sort how you want.

    2. Set the Sorted property to False and sort the data yourself, e.g.
    vb.net Code:
    1. Array.Sort(myArray)
    2. Array.Reverse(myArray)
    3. myListBox.DataSource = myArray
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Sorting a Listbox Descending

    Setting the listbox' sorted property to true will sort it in an ascending order, but since you need it in a descending order, I suppose you could set Sorted to true, and after adding the items you could do this:
    vb Code:
    1. Dim arr(ListBox1.Items.Count - 1) As Object
    2.  
    3.         ListBox1.Items.CopyTo(arr, 0)
    4.         Array.Reverse(arr)
    5.         ListBox1.Items.Clear()
    6.         ListBox1.Items.AddRange(arr)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Re: [2005] Sorting a Listbox Descending

    Quote Originally Posted by Atheist
    Setting the listbox' sorted property to true will sort it in an ascending order, but since you need it in a descending order, I suppose you could set Sorted to true, and after adding the items you could do this:
    vb Code:
    1. Dim arr(ListBox1.Items.Count - 1) As Object
    2.  
    3.         ListBox1.Items.CopyTo(arr, 0)
    4.         Array.Reverse(arr)
    5.         ListBox1.Items.Clear()
    6.         ListBox1.Items.AddRange(arr)
    Thanks this worked nicely.

  5. #5
    New Member
    Join Date
    Mar 2008
    Location
    India
    Posts
    1

    Re: [RESOLVED] [2005] Sorting a Listbox Descending

    Better solutions is,
    ListBox1.Items.Insert(0, "Hi")

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