Howard
Sun Oct 24 09:02:26 CDT 2004
"Helmut Weber" <elmkqznfwvccbf@mailinator.com> wrote in message
news:93vmn094lcalkgt725lun53lgjp0l83rvn@4ax.com...
> Hi,
> one way, which I don't like, would be wordbasic.sortarray,
> which doesn't sort correctly anyway. Thanks to Howard Kaikow.
> There are multiple other ways, explained in every good book
> on programming, and here
>
http://www.standards.com/Sorting/SortPerformanceComparison-Description.html
Please use
http://www.standards.com/index.html?Sorting
If you have JavaScript enabled, that will take directly to the right web
page.
If you do not have JavaScript enabled, it will take you to index.html, which
has a list of non-JavaScript links.
>
> That's why I haven't commented the code.
>
> Here a simple example on quicksort.
> Sub QuickSort(anArray As Variant, a As Long, z As Long)
> ' a = first element to be sorted
> ' z = last element to be sorted
> Dim aTmp As Long
> Dim zTmp As Long
> Dim m As Variant
> Dim y As Variant
> Dim i As Long
> aTmp = a
> zTmp = z
> m = anArray((a + z) / 2) ' mid element
> While (aTmp <= zTmp)
> While (anArray(aTmp) < m And aTmp < z)
> aTmp = aTmp + 1
> Wend
> While (m < anArray(zTmp) And zTmp > a)
> zTmp = zTmp - 1
> Wend
> If (aTmp <= zTmp) Then
> y = anArray(aTmp)
> anArray(aTmp) = anArray(zTmp)
> anArray(zTmp) = y
> aTmp = aTmp + 1
> zTmp = zTmp - 1
> End If
> Wend
> If (a < zTmp) Then QuickSort anArray, a, zTmp
> If (aTmp < z) Then QuickSort anArray, aTmp, z
> End Sub
> ' ---
> Sub TestSort()
> Dim i As Integer
> Dim a() As String
> With Application.FileSearch
> .NewSearch
> .LookIn = "c:\test"
> .FileName = "*.*"
> .SearchSubFolders = True
> 'reference to office library required
> .Execute msoSortByLastModified
> ReDim a(.FoundFiles.Count - 1)
> For i = 0 To .FoundFiles.Count - 1
> a(i) = .FoundFiles(i + 1)
> Next
> QuickSort a(), 0, UBound(a)
> For i = 0 To UBound(a)
> ActiveDocument.Range.InsertAfter a(i) & vbCr
> ' getting the index
> If a(i) = "C:\test\5.doc" Then MsgBox "index = " & i
> Next
> End With
> End Sub
> ---
> Greetings from Bavaria, Germany
> Helmut Weber, MVP
> "red.sys" & chr(64) & "t-online.de"
> Word XP, Win 98
>
http://word.mvps.org/