Hi Greg,
That's great -- works as intended, and I don't see any problems. I
applaud your enthusiasm!
My only comment is that, while it's nice to know how QuickSort works,
this code is overkill on a couple of levels.
First, there is an array-sort routine built into VBA, more or less.
It's the SortArray method of the WordBasic object, which encapsulates
most of the macro language from Word 6/Word 95. The "documentation" is
at http://word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm. All you
need is to replace the line
QuickSort oFldDDArray, a, z
with the line
WordBasic.SortArray oFldDDArray
and then you can remove the entire QuickSort and Swap procedures, and
delete the declarations and assignments of the variables a and z. It's
just that much less code to maintain.
On a more computer-science level, QuickSort isn't necessarily a good
choice for the job when the array is known to be 25 or fewer items
*and* it's known to be almost sorted already, with only one item out
of order. The reason is that QuickSort has a high "overhead" of
computing sorted sub-arrays and choosing the "pivot point" iMid, and
its worst performance occurs with arrays that are already sorted. It's
more appropriate for a large array (say, more than 100 items) that's
most likely to be in random order.
In this case, I'd suggest that a more efficient algorithm would be:
- Store the current dropdown list in the array.
- Clear the dropdown.
- Starting from the LBound of the array, add the array items in order
to the dropdown list while the current array item is less than the new
item.
- Add the new item to the dropdown list.
- Add the rest of the array to the dropdown list.
This will involve a maximum of 23 comparisons (when the new item is
larger than any of the existing ones, not counting the "Other\Add"
entry), with an average of 12 comparisons.
On a practical level, of course, none of these considerations are
going to make a difference of more than a few millseconds in the
execution time.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
>Jay,
>
[quoted text clipped - 102 lines]
>vItems(iItem1) = vTemp
>End Sub
Greg Maxey - 30 Mar 2005 03:15 GMT
That's is great. Thanks.
I think I follow you on the alternate method that you suggested. Something
like this:
'Clear existing list
oFld.DropDown.ListEntries.Clear
For i = LBound(oFldDDArray) To UBound(oFldDDArray)
If oFldDDArray(i) < oFldDDArray(numEntries - 1) Then
oFld.DropDown.ListEntries.Add oFldDDArray(i)
Else
oFld.DropDown.ListEntries.Add oFldDDArray(numEntries - 1)
Exit For
End If
Next i
For j = i To UBound(oFldDDArray) - 1
oFld.DropDown.ListEntries.Add oFldDDArray(j)
Next j
'Restore "Other\Add" entry at end of list
oFld.DropDown.ListEntries.Add "Other\Add"
'Display result
oFld.Result = newEntry
This seems to work. I like the abbreviate sort method better.
Thanks for all the help.
> - Store the current dropdown list in the array. (No problem)
> - Clear the dropdown. (No problem
> - Starting from the LBound of the array, add the array items in order
> to the dropdown list while the current array item is less than the new
> item.
> - Add the new item to the dropdown list.
> - Add the rest of the array to the dropdown list.

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
> Hi Greg,
>
[quoted text clipped - 148 lines]
>> vItems(iItem1) = vTemp
>> End Sub