Listbox - sorting alphabetically?

Listbox - sorting alphabetically?

Anonymous
Not applicable
716 Views
6 Replies
Message 1 of 7

Listbox - sorting alphabetically?

Anonymous
Not applicable
Hi,

Simple question, I've populated a listbox with layer names but they are in no certain order. I thought that the listbox has a sorted property but it wont let me do

Listbox1.sorted=true

Any help appreciated

Al
0 Likes
717 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
I'm not aware of any sort property for listboxes. I think you'd have to sort them first. I haven't done it, but if you have them in an array, I've seen a few sorting functions on the net, then just pass it to the listbox with the List property. HTH "Pienpeas" wrote in message news:26404003.1088086651697.JavaMail.jive@jiveforum2.autodesk.com... > Hi, > > Simple question, I've populated a listbox with layer names but they are in no certain order. I thought that the listbox has a sorted property but it wont let me do > > Listbox1.sorted=true > > Any help appreciated > > Al
0 Likes
Message 3 of 7

Anonymous
Not applicable
VBA ListBox in ACAD does not do sorting for you. You have do it yourself: put all layers' name in an string array; choose one of your favorite sorting algrithm and write your sorting code; sort the array, and then popualte the listbox/combobox. Actaully, you do not have to write your onw sorting code, there are a lot ready to use sorting algrithms on the net. You can google some VB/VBA related news group for "sorting". Here is one example of what I have found: Public Sub ShellSort(ByRef A() As Variant, _ ByVal Lb As Long, ByVal Ub As Long) Dim n As Long, h As Long Dim i As Long, j As Long Dim t As Variant ' ' ***** If you are using this routine to sort string data and you want ' it to be case insensitive (ie "cat" comes before "Dog") then you ' must place the line "Option Compare Text" in the (general) ' (declarations) section of the Form or Module in which this routine ' is declared ' ******************************* ' ' We allow the user to send his own start and end positions to ' this routine in case he wants to sort only part of the array ' (which is a common requirement) so we must first check and ' adjust those parameters in case he has given us silly numbers! If Ub > UBound(A) Then Ub = UBound(A) If Lb < LBound(A) Then Lb = LBound(A) If Lb >= Ub Then Exit Sub ' ' now sort array (from element lb to element ub) ' first calculate the largest increment n = Ub - Lb + 1 h = 1 If n > 13 Then Do While h < n h = 3 * h + 1 Loop h = h \ 3 If h > (n * 0.8) Then h = h \ 3 End If ' repeatedly do an insertion sort using ' values of h from its initial value down ' to a value of 1 Do While h > 0 For i = Lb + h To Ub t = A(i) For j = i - h To Lb Step -h If A(j) <= t Then Exit For A(j + h) = A(j) Next j A(j + h) = t Next i h = h \ 3 Loop End Sub "Pienpeas" wrote in message news:26404003.1088086651697.JavaMail.jive@jiveforum2.autodesk.com... > Hi, > > Simple question, I've populated a listbox with layer names but they are in no certain order. I thought that the listbox has a sorted property but it wont let me do > > Listbox1.sorted=true > > Any help appreciated > > Al
0 Likes
Message 4 of 7

Anonymous
Not applicable
The VB version of the listbox has a Sort property. The VBA version does not. "Pienpeas" wrote in message news:26404003.1088086651697.JavaMail.jive@jiveforum2.autodesk.com... > Hi, > > Simple question, I've populated a listbox with layer names but they are in no certain order. I thought that the listbox has a sorted property but it wont let me do > > Listbox1.sorted=true > > Any help appreciated > > Al
0 Likes
Message 5 of 7

Anonymous
Not applicable
Most listboxes don't have very many entries in them. For lists of under 1000 items a Bubble Sort is recommended.

Regards

Wayne Ivory
IT Analyst Programmer
Wespine Industries Pty Ltd
0 Likes
Message 6 of 7

Anonymous
Not applicable
Thanks guys, its obviously not as simple as i thought!
0 Likes
Message 7 of 7

Anonymous
Not applicable
I ended up using a combobox for my list of layers, too much hassle w/a listbox. Dim intTeller As Integer Dim intRuns As Integer Dim varTemp As Variant Dim entry As AcadLayer Dim arrLayers() As Variant Dim iCount As Integer For Each entry In ThisDrawing.Layers ReDim Preserve arrLayers(iCount) arrLayers(iCount) = entry.Name iCount = iCount + 1 Next For intRuns = 0 To UBound(arrLayers()) - 1 For intTeller = 0 To UBound(arrLayers()) - 1 If arrLayers(intTeller) > arrLayers(intTeller + 1) Then varTemp = arrLayers(intTeller + 1) arrLayers(intTeller + 1) = arrLayers(intTeller) arrLayers(intTeller) = varTemp End If Next intTeller Next intRuns For intRuns = 0 To UBound(arrLayers()) cmbLayers.AddItem arrLayers(intRuns) Next intRuns "Pienpeas" wrote in message news:26404003.1088086651697.JavaMail.jive@jiveforum2.autodesk.com... > Hi, > > Simple question, I've populated a listbox with layer names but they are in no certain order. I thought that the listbox has a sorted property but it wont let me do > > Listbox1.sorted=true > > Any help appreciated > > Al
0 Likes