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

Re: how to sort an array of strings? by Helmut

Helmut
Sun Oct 24 05:11:33 CDT 2004

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/

Re: how to sort an array of strings? by Howard

Howard
Sun Oct 24 08:59:19 CDT 2004

See http://www.standards.com/index.html?Sorting.

--
http://www.standards.com/; See Howard Kaikow's web site.
"wcc" <wccppp@yahoo.com> wrote in message
news:92b1f34c.0410240018.45bc6191@posting.google.com...
> 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



Re: how to sort an array of strings? by Howard

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/