# Sorting And Ordering with a listbox?

Page 1 of 1

## 7 Replies - 873 Views - Last Post: 25 January 2014 - 10:49 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=338791&amp;s=ef32a47aaa8cd173031400042512db7b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Leftic

Reputation: 1
• Posts: 20
• Joined: 18-July 13

# Sorting And Ordering with a listbox?

Posted 23 January 2014 - 12:27 PM

Alright, I looked around Google and couldnt find a simple solution to what I want to do.

"Sort A - Z"
"Sort Z - A"
"Sort 0 - 9"
"Sort 9 - 0"
... You get the idea

So, as I was coding it, I realized, that there is no simple command for listboxes like "listbox1.items.order(0,9)" to say starting index being 0, and ending index being 9, so every item within that range will then be ordered from 0 - 9

So, how might I set sorting/ordering from 0 - 9, 9 - 0, a - z, z - a, A - Z, and Z - A?

Thanks for any help in advance.

Is This A Good Question/Topic? 0

## Replies To: Sorting And Ordering with a listbox?

### #2 modi123_1

• Suitor #2

Reputation: 10839
• Posts: 42,465
• Joined: 12-June 08

## Re: Sorting And Ordering with a listbox?

Posted 23 January 2014 - 12:31 PM

I would figure you need to create a custom listbox (that inherits listbox) and override the sort mechanism.

MSDN seems to agree with that idea.

Quote

Notes to Inheritors

You can override this method in your derived class to provide your own sorting routine. Access the results of the overridden Sort method by setting the Sorted property to true. When adding items to a ListBox, it is more efficient to sort the items first and then add new items.

http://msdn.microsof...=vs.110%29.aspx

### #3 Minimalist

Reputation: 30
• Posts: 207
• Joined: 13-November 13

## Re: Sorting And Ordering with a listbox?

Posted 23 January 2014 - 02:15 PM

The listbox has a sorted property. So in your radiobutton click method you can set the listbox to ...sorted=true whic will sort the list box. To rverse the sort you put in the 2nd radiobutton ... you create an array, copy the sorted list into the array,set your lisbox....sorted = false , clear the listbox and copy the items of the array from the highest index to = into the listbox. This takes care of your words.

• Home Owner

Reputation: 460
• Posts: 2,322
• Joined: 11-May 12

## Re: Sorting And Ordering with a listbox?

Posted 24 January 2014 - 06:46 AM

Another option is to create a List(Of String) and populate that with your listbox items, then sort the List(Of String) using .sort, then clear and re-add the items back into the listbox.

### #5 Leftic

Reputation: 1
• Posts: 20
• Joined: 18-July 13

## Re: Sorting And Ordering with a listbox?

Posted 24 January 2014 - 03:15 PM

None of that seemed to help. Thank you for the suggestions though.

### #6 modi123_1

• Suitor #2

Reputation: 10839
• Posts: 42,465
• Joined: 12-June 08

## Re: Sorting And Ordering with a listbox?

Posted 24 January 2014 - 03:22 PM

### #7 CharlieMay

• This space intentionally left blank

Reputation: 1721
• Posts: 5,636
• Joined: 25-September 09

## Re: Sorting And Ordering with a listbox?

Posted 24 January 2014 - 03:29 PM

You could also show us some results from your attempts and show why they didn't work for you. Perhaps, it's something to the effect that you list sorted 0-9 like
1
11
2
21
3
4
5
6
1
2
3
4
5
6
11
21

which is due to the fact that the listbox is seeing the numbers as a string representation of a number which would cause this kind of sort. So instead of telling us that it didn't work, perhaps you could give a little more insight as to why your attempt is not producing desired results and give someone something more to work with.

### #8 Leftic

Reputation: 1
• Posts: 20
• Joined: 18-July 13

## Re: Sorting And Ordering with a listbox?

Posted 25 January 2014 - 10:49 AM

Theres no actual physical errors, only logical ones.
For example: When I tried to sort it myself, it either did not move anything, or it would just copy the last or 2nd last item to the first item in the list.

And overloading the control wouldnt work because the 6 types of options I want to do requires more then just a simple sort, then sort - 1 (for example for A - Z then Z - A)... At least that I could find while testing it, so I attempted to make my own, which is where it either does nothing or copies the 2nd last item and pastes it to the top.

Here is my code:

```itterate = 0
Try
For Each title In ListBox1.Items
itterate += 1
If ListBox1.Items(itterate).ToString.StartsWith("0") Then
'ListBox1.Items.RemoveAt(itterate)
ListBox1.Items.Insert(1, ListBox1.Items(itterate))
End If
If ListBox1.Items(itterate).ToString.StartsWith("1") Then
'ListBox1.Items.RemoveAt(itterate)
ListBox1.Items.Insert(2, ListBox1.Items(itterate))
End If
If ListBox1.Items(itterate).ToString.StartsWith("2") Then
'ListBox1.Items.RemoveAt(itterate)
ListBox1.Items.Insert(3, ListBox1.Items(itterate))
End If
Next
Else

End If
Catch
Me.Show()
End Try

```

This is all in the RadioButton1's Check Changed event.
"itterate" was already declared earlier at the top of my code, which is why you do not see a "Dim itterate = 0" there.

I started at 0 instead of -1 because I do not want the very first item of the listbox to ever be affected, as other parts of my program rely on it being and staying as the 0 index.