# Sorting numbers in ListBox, How ?? RSS

## 7 replies

Last post Nov 11, 2009 08:51 AM by atconway

Participant

1291 Points

450 Posts

### Sorting numbers in ListBox, How ??

Hi; I used a ListBox control, and I assifned to it a list of random numbers, then I sorted the ListBox by usign ListBox1.Sort = True BUT the sorting operation is NOT right! Example of Random numbers: 1,5,7,11,12,4 After sorting: 1,11,12,4,5,7 Whixh is not right, How to make it: 1,4,5,7,11,12 Q: How to make a ListBox deal with numbers as numbers NOT as text!? Thanks in advance!
Regards,
CS4Ever
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as so we will ALL know you have been helped.

Member

151 Points

4633 Posts

### Re: Sorting numbers in ListBox, How ??

You can either save values with leading 0's or spaces, or sort them manually and then insert them in the correct order into the list box (setting sort=false).
Starting with ASP.NET 2.0? Look at:
Programming Microsoft Web Forms
My Blog

Participant

1291 Points

450 Posts

### Re: Sorting numbers in ListBox, How ??

Firstly, thanx for your replay. Secondly, Ok, How to save values with leading 0's or spaces?
Regards,
CS4Ever
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as so we will ALL know you have been helped.

Member

151 Points

4633 Posts

### Re: Sorting numbers in ListBox, How ??

```Dim i as Integer Dim s as String Dim s2 as String i=7 s=i.ToString("00") s2=s.TrimStart("0"c) ```S will equal "07" S2 will equal " 7" The TrimStart syntax presumes VB.NET.
Starting with ASP.NET 2.0? Look at:
Programming Microsoft Web Forms
My Blog

Participant

1291 Points

450 Posts

### Re: Sorting numbers in ListBox, How ??

Ok, how to make that with ListBox Control? Sorry, I'm a begineer!
Regards,
CS4Ever
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as so we will ALL know you have been helped.

Member

151 Points

4633 Posts

### Re: Sorting numbers in ListBox, How ??

You can convert, one at a time, the numbers in the array, and then use .Add() on the ListBox to add the value to the list box. There are a number of other ways to do it as well. Do take a shot at it and if you run into problems, post what code you have.
Starting with ASP.NET 2.0? Look at:
Programming Microsoft Web Forms
My Blog

None

0 Points

1 Post

### Re: Sorting numbers in ListBox, How ??

ex)

s = i.ToString("00")

listbox1.Sorted = True

For k As Integer = 1 To listbox1.Items.Count - 1
s = listbox1.Items.Item(k)
s2 = s.TrimStart("0"c)
Next

Star

12060 Points

2742 Posts

### Re: Sorting numbers in ListBox, How ??

Here is a method I use to sort ListBox items.  Just pass in the ListBox control, and all of your items will be sorted:

(The code and explination can also be found at the following link:)

http://allen-conway-dotnet.blogspot.com/2009/11/how-to-sort-items-in-aspnet-listbox.html

```Public Shared Sub SortListBox(ByVal lbxId As ListBox)

Dim t As New List(Of ListItem)()
Dim compare As New Comparison(Of ListItem)(AddressOf CompareListItems)

'Iterate through each ListItem in the Listbox, and add them to the 'List' object
For Each lbItem As ListItem In lbxId.Items
Next

'Sort the List
t.Sort(compare)

'Clear the Listbox passed in, and add the sorted list from above:
lbxId.Items.Clear()

End Sub

Public Shared Function CompareListItems(ByVal li1 As ListItem, ByVal li2 As ListItem) As Integer

'Return the strings in order that have been compared:
Return [String].Compare(li1.Text, li2.Text)

End Function```

...to call it just pass in your ListBox control like below:

```'Sort the ListBox on my .aspx page
SortListBox(Me.lbxNumbers)```

Hope this helps!