Re: Sort Two Dimensioanl Array



Hi Greg,

Does Chris' Bubble Sort work - works in 2003 Autotext.

Routine: BubbleSort
Parameters: ToSort As Variant, Optional SortAscending As Boolean =
True
Description: Sort the given array into order. The sort used is
"bubble sort" and defaults to ascending.

--------------------------------------------------------------------------------

Sub BubbleSort(ToSort As Variant, Optional SortAscending As Boolean = True)
' Chris Rae's VBA Code Archive - http://chrisrae.com/vba
' By Chris Rae, 19/5/99. My thanks to
' Will Rickards and Roemer Lievaart
' for some fixes.
Dim AnyChanges As Boolean
Dim BubbleSort As Long
Dim SwapFH As Variant
Do
AnyChanges = False
For BubbleSort = LBound(ToSort) To UBound(ToSort) - 1
If (ToSort(BubbleSort) > ToSort(BubbleSort + 1) And
SortAscending) _
Or (ToSort(BubbleSort) < ToSort(BubbleSort + 1) And Not
SortAscending) Then
' These two need to be swapped
SwapFH = ToSort(BubbleSort)
ToSort(BubbleSort) = ToSort(BubbleSort + 1)
ToSort(BubbleSort + 1) = SwapFH
AnyChanges = True
End If
Next BubbleSort
Loop Until Not AnyChanges
End Sub
jecwww.ribbonspace.com"Greg Maxey"
<gmaxey@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:%23i%23v$RYVJHA.1224@xxxxxxxxxxxxxxxxxxxxxxx
I have some code that loads each building block from each template (in the
template collection) into a 2 dimensional array and then puts those items
into a UserForm combobox.

How can I sort this array so that the items listed in the combobox are
listed alphabetically? Thanks


Sub BuildList
Dim lngCount As Long
Dim i As Long
Dim j As Long
pStr = ""
lngCount = 0
For Each oTmp In Templates
For i = 1 To oTmp.BuildingBlockEntries.Count
If Left(oTmp.BuildingBlockEntries(i).Name, Len(pStr)) = pStr Then
lngCount = lngCount + 1
End If
Next
Next
If lngCount > 0 Then
ReDim bbArray(0 To lngCount - 1, 1 To 4)
Else
ReDim bbArray(0)
bbArray(0) = "No matching building block entries"
Exit Sub
End If
j = 0
'Load those building blocks into an array
For Each oTmp In Templates
For i = 1 To oTmp.BuildingBlockEntries.Count
If Left(oTmp.BuildingBlockEntries(i).Name, Len(pStr)) = pStr Then
bbArray(j, 1) = oTmp.BuildingBlockEntries(i).Name
bbArray(j, 2) = oTmp.FullName
bbArray(j, 3) = oTmp.BuildingBlockEntries(i).Value
bbArray(j, 4) = oTmp.BuildingBlockEntries(i).Index
j = j + 1
End If
Next
Next
Me.cbBuildingBlocks.List = bbArray()
End Sub


--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org





.



Relevant Pages

  • Re: Help me with references
    ... >> efficiently than you can with bubble sort. ... > Bubblesort accomplishes this faster than insertion sort. ... your "almost sorted array" with one comparison. ...
    (comp.lang.java.help)
  • Re: Sorting
    ... Then go back to the start of the array and do it again, until you make a clean sweep without swapping. ... This algorithm is called bubblesort. ... less efficient than selection sort or insertion sort, ...
    (comp.lang.c)
  • Re: Sorting
    ... The easiest, probably, is to go through the array. ... This algorithm is called bubblesort. ... bother to think and implement such a horrid algorithm. ... less efficient than selection sort or insertion sort, ...
    (comp.lang.c)
  • Re: quick sort
    ... The following is the quick sort function which i have seen in my note ... choice of which elements to swap. ... toward the beginning of the array. ... using a bubblesort, which has fewer overheads. ...
    (comp.lang.c)
  • Re: Help me with references
    ... I can't think of any situation in which bubble sort is ... > best choice for a sorting algorithm in a real machine. ... Quicksort is optimized for large Ns. ... I picked Bubblesort because your original post implied something along ...
    (comp.lang.java.help)

Loading