WordBasic.SortArray seems to fail for sorting strings

H

Howard Kaikow

Over the past few daze, I've noticed that WordBasic.SortArray does not
always sort strings correctly.

The output of the code below demonstrates that the sort is incorrect, e.g.,
"com" should not appear prior to "convertAnswer" , and "Command1" should not
follow "convertAnswer" in the sorted data.

Option Explicit
'This example demonstrates that WordBasic.SortArray does not sort strings
correctly.
Private Sub WBReal()
Dim a(35) As String
Dim i As Long
Dim strData() As String

a(0) = "Accessing"
a(1) = "Recordset"
a(2) = "provide"
a(3) = "hard"
a(4) = "NotOverridable"
a(5) = "Layout"
a(6) = "your"
a(7) = "servername"
a(8) = "ProjectInstaller"
a(9) = "Premium"
a(10) = "have"
a(11) = "cboCountry"
a(12) = "Gray"
a(13) = "Reset"
a(14) = "tuned"
a(15) = "noticed"
a(16) = "Command1"
a(17) = "com"
a(18) = "State"
a(19) = "problem"
a(20) = "pasting"
a(21) = "responsible"
a(22) = "circles"
a(23) = "returning"
a(24) = "rewrite"
a(25) = "occurred"
a(26) = "convertAnswer"
a(27) = "your"
a(28) = "ADO 's"
a(29) = "Word"
a(30) = "reducing"
a(31) = "Misc"
a(32) = "categorized"
a(33) = "preface"
a(34) = "Alan"
a(35) = "teams"
strData = a

WordBasic.SortArray strData(), False
For i = 0 To UBound(strData)
Debug.Print strData(i)
Next i
End Sub
 
H

Howard Kaikow

I could have more easily stated:

"The output of the code below demonstrates that the sort is incorrect, e.g.,
"convertAnswer" should not appear prior to "Command1" in the sorted data."

It seems that WB not follow the model used by Option Compare Text or Option
Compare Binary?
 
P

Peter Hewett

Hi Howard

On the basis of:
"convertAnswer" > "Command1"
and
strcomp("convertAnswer" , "Command1",vbTextCompare) = 1

you're dead right. The WordBasic sort is obviously gimmicked to place lower
case before upper case despite their numeric character values. Now you know
why the Wordbasic sort is slow!

Cheers - Peter
 
H

Howard Kaikow

That would not account for the speed difference.
Problem is deeper than that.

I expect that the speed would be even worse were VBA SortArray not using
memory movement tricks, at least, I sure hope that it is using memcopy, etc.

More importantly, there needs to be "documentation" of just what SortArray
does. Heck, I should be able to write brute force code to verify the
correctness of the sort, however, if the code does not follow TextCompare or
BinaryCompare, I'm outta luck..
 
P

Peter Hewett

Hi Howard

As with all sorting/searching algorithms they are incredibly data dependent,
by this I mean the quantity/volume of data and the distribution of the data.
I guess that's why there's whole books dedicated to these topics alone!

But I was joking!

Good luck + Cheers - Peter
 
H

Howard Kaikow

The issue is, pardon me if I use a word, no pun intended, that may not be in
MSFT's dictionary.
It makes no sense to provide a sorting routine without explicit
documentation, oops, there's that dirty word, of the behavior of the sorting
code.
 

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