how to sort an array of strings?

W

wcc

Hello group,

Beginner here with 2 questions.

1. I have an array of strings which are word document names. Is there
a built-in function in word vba that I can use to sort them
alphabetically?

2. If I have an array of strings, say ("Mon" "Tue" "Wed" "Thu") and I
have an item, say "Wed" which belongs to that array. Is there a
built-in function to determine the index of this item in the array?
(in this case it should return 2).

Thanks very much for your help.

-wcc
 
H

Helmut Weber

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

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/
 
H

Howard Kaikow

Helmut Weber said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top