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.
Printable View
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.
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:
Array.Sort(myArray) Array.Reverse(myArray) myListBox.DataSource = myArray
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:
Dim arr(ListBox1.Items.Count - 1) As Object ListBox1.Items.CopyTo(arr, 0) Array.Reverse(arr) ListBox1.Items.Clear() ListBox1.Items.AddRange(arr)
Thanks this worked nicely.Quote:
Originally Posted by Atheist
Better solutions is,
ListBox1.Items.Insert(0, "Hi")