LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 345
Default Bubble Sort failure, Strcomp with binary compare

Hi All,
I have a bubble sort failing when
"a" seems to compare greater then "B"
in a binary text compare. There are notes
in the bubble sort loop below.

Other more 'complicated' data tests well, also
in the notes.

Help is appreciated.
Thanks,
Neal Zimm


Sub zBubble_Sort()
Dim CompareHow As VbCompareMethod

Dim Ix As Long
Dim Jx As Long
Dim Lo As Long, Hi As Long

Dim KeyAy As Variant
Dim sHold As String
Dim sInput As String
Dim sOutput As String

' All binary compares
' KeyAy = Array("a", "A") 'good, result is Aa
' KeyAy = Array("b", "B") 'good, result is Bb
' KeyAy = Array("b", "a", "A") 'good, result = Aab
' KeyAy = Array("B", "a", "A") 'NOPE, result = ABa

CompareHow = vbBinaryCompare
''CompareHow = vbTextCompare

KeyAy = Array("b", "a", "A", "B") 'crap, result = ABab expect AaBb ??
GoSub SortTest

'the above has a problem, see sort. Bummer is that
'the data below works as expected.

KeyAy = Array("z i m", "z i M", "z I m", "z I M", "Z I m", "Z I M")
GoSub SortTest
For Ix = Lo To Hi
sOutput = sOutput & KeyAy(Ix) & vbCr
Next Ix
MsgBox sOutput
Exit Sub

SortTest:
Lo = LBound(KeyAy)
Hi = UBound(KeyAy)

For Ix = Lo To Hi
sInput = sInput & KeyAy(Ix)
Next Ix

'sort ascending
For Ix = Lo To (Hi - 1)
For Jx = (Ix + 1) To Hi
'The failure is @ Ix=2 "a", it compares greater
' than Jx=4 of "B" WHY ??
If StrComp(KeyAy(Ix), KeyAy(Jx), CompareHow) = 1 Then
sHold = KeyAy(Jx)
KeyAy(Jx) = KeyAy(Ix)
KeyAy(Ix) = sHold
End If
Next Jx
Next Ix


For Ix = Lo To Hi
sOutput = sOutput & KeyAy(Ix)
Next Ix
MsgBox sInput & vbCr & vbCr & sOutput
sInput = ""
sOutput = ""
Return
End Sub

--
Neal Z
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
StrComp Rick Excel Programming 4 August 17th 07 11:48 PM
binary sort? Herbert Excel Programming 2 November 10th 05 07:55 AM
Sort data failure Robert Pollock[_2_] Excel Programming 1 February 16th 04 01:27 PM
Calling Bubble Sort Function [email protected] Excel Programming 6 December 26th 03 07:27 PM


All times are GMT +1. The time now is 11:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"