# Can ListBox.Sorted Property be changed?

• December 20th, 2003, 10:55 AM
winke04
Can ListBox.Sorted Property be changed?
Can I set the sorted property true/false in code or is it only available in properties?

I've tried ListBox.Sorted = True and it says it's a read only property.
• December 20th, 2003, 12:08 PM
Hobbit K
Quote:

Originally posted by winke04

I've tried ListBox.Sorted = True and it says it's a read only property.
Read-only is READ ONLY ... you cannot write to or manipulate a read only property.
• December 21st, 2003, 12:01 AM
Jinto
But if you really want it sorted you can sort it using something like the Bubble Sort algorithm, I have only used it with Numerics, but I'm sure you can do it with strings too. You would just have to modify it to sort strings.

Code:

```Public Sub BubbleSort(ByRef nSort() As Integer, Optional stAccending As Boolean = True)     Dim bFound As Boolean     Dim t As Integer     Dim j As Integer     'Dim passes As Long     'Dim replacments As Long         bFound = True             Do Until bFound = False         bFound = False         'passes = passes + 1         For j = UBound(nSort) - 1 To LBound(nSort) Step -1             Select Case stAccending                 Case True                     If nSort(j + 1) < nSort(j) Then                         DoEvents                         bFound = True                         t = nSort(j)                         nSort(j) = nSort(j + 1)                         nSort(j + 1) = t                         'replacments = replacments + 1                     End If                 Case False                     If nSort(j + 1) > nSort(j) Then                         DoEvents                         bFound = True                         t = nSort(j)                         nSort(j) = nSort(j + 1)                         nSort(j + 1) = t                         'replacments = replacments + 1                     End If             End Select         Next     Loop End Sub```
• December 21st, 2003, 12:43 AM
WizBang
QuickSort will be faster. However, you can just set it to be sorted at design time, then use an index to add items when you don't want it sorted. For instance, the following will always add to the bottom of the list, even when it is sorted:
List1.AddItem "New Line", List1.ListCount
• December 21st, 2003, 01:27 AM
Jinto
Hmm.. I never heard of quick sort, I've only heard of Bubble Sort, Insertion Sort, Selection Sort, and two others I can't remember right now.. If quick sort has something to do with creating some kind of partition of an array then maybe I have seen it before.

I will have to look into that quick sort, will make all my huge sorts faster :D
• December 21st, 2003, 08:04 AM
winke04
I copped out for now by adding another listbox. One is sorted and one is not.

I have a large Access DB to keep track of all my MP3 files. I have tables of Artists, Albums, and Songs. When I list all songs I want them in alpha order. When I list the songs on a selected album I want them in the order they are on the album which is the order they are stored in the DB. If I could have just set the Sorted property at run time I could have used the same listbox. As it turned out there weren't too many places I had to do an If Then Else to use either listbox.

I'll sure keep the sort info in mind though. I know there will be uses for it coming up.

Now if I can just figure out this packaging and deployment. What a pain that is.
• December 21st, 2003, 05:43 PM
WizBang
Well, then you should be able to leave it sorted, and just specify the index as stated above, and it will be as you want.