PC Review


Reply
Thread Tools Rate Thread

Bubble Sort failure, Strcomp with binary compare

 
 
Neal Zimm
Guest
Posts: n/a
 
      14th Mar 2010
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
 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      14th Mar 2010
That is because a is greater than B in ASCII. IF you want otherwise you will
need to shift to a consistent form, UCase or LCase.

--

HTH

Bob

"Neal Zimm" <(E-Mail Removed)> wrote in message
news:46D21FA6-1BA7-482C-88E6-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Bernard Liengme
Guest
Posts: n/a
 
      14th Mar 2010
In the SortTest sub use
If StrComp(UCase(KeyAy(Ix)), UCase(KeyAy(Jx)), CompareHow) = 1 Then

Note the use of UCASE
But A, B, a. B in A1:A4; In B1 enter =CODE(A1);copu down the column. Now do
you see why the sort works the way it does?
best wishes
--
Bernard Liengme
Microsoft Excel MVP
http://people.stfx.ca/bliengme

"Neal Zimm" <(E-Mail Removed)> wrote in message
news:46D21FA6-1BA7-482C-88E6-(E-Mail Removed)...
> 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


 
Reply With Quote
 
Neal Zimm
Guest
Posts: n/a
 
      15th Mar 2010
Well Bob,
Crap, not at you, just the situation. If i wanted to use U and L case, I
wouldn't be bothering with the binary compare, the sort works just fine with
the text compare.

So if you want an apha sort in VBA, where case matters, do you have to
build your own ?

Thanks and be well.
--
Neal Z


"Bob Phillips" wrote:

> That is because a is greater than B in ASCII. IF you want otherwise you will
> need to shift to a consistent form, UCase or LCase.
>
> --
>
> HTH
>
> Bob
>
> "Neal Zimm" <(E-Mail Removed)> wrote in message
> news:46D21FA6-1BA7-482C-88E6-(E-Mail Removed)...
> > 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

>
>
> .
>

 
Reply With Quote
 
Neal Zimm
Guest
Posts: n/a
 
      15th Mar 2010
Cher Bernard,
Merci.
Lcase and Ucase do me no good.

The sort works fine with option compare text, but I wanted "more".

If you need a case sensitive sort, (and I forgot about the ascii
collating sequence) what's the best way without writing to a worksheet and
sorting there?

thanks much.
Neal

--
Neal Z


"Bernard Liengme" wrote:

> In the SortTest sub use
> If StrComp(UCase(KeyAy(Ix)), UCase(KeyAy(Jx)), CompareHow) = 1 Then
>
> Note the use of UCASE
> But A, B, a. B in A1:A4; In B1 enter =CODE(A1);copu down the column. Now do
> you see why the sort works the way it does?
> best wishes
> --
> Bernard Liengme
> Microsoft Excel MVP
> http://people.stfx.ca/bliengme
>
> "Neal Zimm" <(E-Mail Removed)> wrote in message
> news:46D21FA6-1BA7-482C-88E6-(E-Mail Removed)...
> > 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

>
> .
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      15th Mar 2010
So why not use text compare instead of binary.

Regards,
Peter T

"Neal Zimm" <(E-Mail Removed)> wrote in message
news:216D8124-ACAE-4520-BC63-(E-Mail Removed)...
> Cher Bernard,
> Merci.
> Lcase and Ucase do me no good.
>
> The sort works fine with option compare text, but I wanted "more".
>
> If you need a case sensitive sort, (and I forgot about the ascii
> collating sequence) what's the best way without writing to a worksheet and
> sorting there?
>
> thanks much.
> Neal
>
> --
> Neal Z
>
>
> "Bernard Liengme" wrote:
>
>> In the SortTest sub use
>> If StrComp(UCase(KeyAy(Ix)), UCase(KeyAy(Jx)), CompareHow) = 1 Then
>>
>> Note the use of UCASE
>> But A, B, a. B in A1:A4; In B1 enter =CODE(A1);copu down the column. Now
>> do
>> you see why the sort works the way it does?
>> best wishes
>> --
>> Bernard Liengme
>> Microsoft Excel MVP
>> http://people.stfx.ca/bliengme
>>
>> "Neal Zimm" <(E-Mail Removed)> wrote in message
>> news:46D21FA6-1BA7-482C-88E6-(E-Mail Removed)...
>> > 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

>>
>> .
>>



 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      15th Mar 2010

I have an implementation of QSort on my web site at
http://www.cpearson.com/Excel/QSort.htm . You can download the module
at http://www.cpearson.com/zips/modQSortInPlace.zip . It handles both
case sensitive and case insensitive sorts in either ascending or
descending order. QSort is much faster than a bubble sort. The code
can handle sorting both strings and numerics and it can handle sorting
only a subset of an array.

You would call QSortInPlace with code like

Sub AAA()
Dim Arr(1 To 5) As String
Dim N As Long
Arr(1) = "z"
Arr(2) = "A"
Arr(3) = "a"
Arr(4) = "Z"
Arr(5) = "m"
QSortInPlace InputArray:=Arr, CompareMode:=vbBinaryCompare
For N = 1 To 5
Debug.Print Arr(N)
Next N
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com




On Sun, 14 Mar 2010 17:44:01 -0700, Neal Zimm <(E-Mail Removed)>
wrote:

>Cher Bernard,
> Merci.
> Lcase and Ucase do me no good.
>
> The sort works fine with option compare text, but I wanted "more".
>
> If you need a case sensitive sort, (and I forgot about the ascii
>collating sequence) what's the best way without writing to a worksheet and
>sorting there?
>
>thanks much.
>Neal

 
Reply With Quote
 
Neal Zimm
Guest
Posts: n/a
 
      16th Mar 2010
Peter, I need to build a "complementary" string in order to get the
descending functionality to work. the opposite of a space is the "~"
character. The text compare method did not recogize it. I'll be posting the
working code I've just finished later today. Thanks.

--
Neal Z


"Peter T" wrote:

> So why not use text compare instead of binary.
>
> Regards,
> Peter T
>
> "Neal Zimm" <(E-Mail Removed)> wrote in message
> news:216D8124-ACAE-4520-BC63-(E-Mail Removed)...
> > Cher Bernard,
> > Merci.
> > Lcase and Ucase do me no good.
> >
> > The sort works fine with option compare text, but I wanted "more".
> >
> > If you need a case sensitive sort, (and I forgot about the ascii
> > collating sequence) what's the best way without writing to a worksheet and
> > sorting there?
> >
> > thanks much.
> > Neal
> >
> > --
> > Neal Z
> >
> >
> > "Bernard Liengme" wrote:
> >
> >> In the SortTest sub use
> >> If StrComp(UCase(KeyAy(Ix)), UCase(KeyAy(Jx)), CompareHow) = 1 Then
> >>
> >> Note the use of UCASE
> >> But A, B, a. B in A1:A4; In B1 enter =CODE(A1);copu down the column. Now
> >> do
> >> you see why the sort works the way it does?
> >> best wishes
> >> --
> >> Bernard Liengme
> >> Microsoft Excel MVP
> >> http://people.stfx.ca/bliengme
> >>
> >> "Neal Zimm" <(E-Mail Removed)> wrote in message
> >> news:46D21FA6-1BA7-482C-88E6-(E-Mail Removed)...
> >> > 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
> >>
> >> .
> >>

>
>
> .
>

 
Reply With Quote
 
Neal Zimm
Guest
Posts: n/a
 
      16th Mar 2010
As always, thanks much Chip, I'll be looking at your site later today.

I'll be posting the code I just finished, here in about 10 minutes. I
should know by now to look at your site first, but I wanted the knowledge in
building the sort that I wrote.

I'm kinda proud of it, it sorts an array on any or all of its columns , each
column can be ascending or descending, but you can be sure I'll check yours
out.

Thanks again.
--
Neal Z


"Chip Pearson" wrote:

>
> I have an implementation of QSort on my web site at
> http://www.cpearson.com/Excel/QSort.htm . You can download the module
> at http://www.cpearson.com/zips/modQSortInPlace.zip . It handles both
> case sensitive and case insensitive sorts in either ascending or
> descending order. QSort is much faster than a bubble sort. The code
> can handle sorting both strings and numerics and it can handle sorting
> only a subset of an array.
>
> You would call QSortInPlace with code like
>
> Sub AAA()
> Dim Arr(1 To 5) As String
> Dim N As Long
> Arr(1) = "z"
> Arr(2) = "A"
> Arr(3) = "a"
> Arr(4) = "Z"
> Arr(5) = "m"
> QSortInPlace InputArray:=Arr, CompareMode:=vbBinaryCompare
> For N = 1 To 5
> Debug.Print Arr(N)
> Next N
> End Sub
>
> Cordially,
> Chip Pearson
> Microsoft Most Valuable Professional,
> Excel, 1998 - 2010
> Pearson Software Consulting, LLC
> www.cpearson.com
>
>
>
>
> On Sun, 14 Mar 2010 17:44:01 -0700, Neal Zimm <(E-Mail Removed)>
> wrote:
>
> >Cher Bernard,
> > Merci.
> > Lcase and Ucase do me no good.
> >
> > The sort works fine with option compare text, but I wanted "more".
> >
> > If you need a case sensitive sort, (and I forgot about the ascii
> >collating sequence) what's the best way without writing to a worksheet and
> >sorting there?
> >
> >thanks much.
> >Neal

> .
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      16th Mar 2010
Not sure what you mean by - the opposite of a space is the "~", however
StrComp with textCompare certainly does recognize the "~", indeed in this
range same as Excel's compare order (which is not entirely the same as
VB's).

Regards,
Peter T


"Neal Zimm" <(E-Mail Removed)> wrote in message
news:5D1149E2-BF66-4CD3-92EB-(E-Mail Removed)...
> Peter, I need to build a "complementary" string in order to get the
> descending functionality to work. the opposite of a space is the "~"
> character. The text compare method did not recogize it. I'll be posting
> the
> working code I've just finished later today. Thanks.
>
> --
> Neal Z
>
>
> "Peter T" wrote:
>
>> So why not use text compare instead of binary.
>>
>> Regards,
>> Peter T
>>
>> "Neal Zimm" <(E-Mail Removed)> wrote in message
>> news:216D8124-ACAE-4520-BC63-(E-Mail Removed)...
>> > Cher Bernard,
>> > Merci.
>> > Lcase and Ucase do me no good.
>> >
>> > The sort works fine with option compare text, but I wanted "more".
>> >
>> > If you need a case sensitive sort, (and I forgot about the ascii
>> > collating sequence) what's the best way without writing to a worksheet
>> > and
>> > sorting there?
>> >
>> > thanks much.
>> > Neal
>> >
>> > --
>> > Neal Z
>> >
>> >
>> > "Bernard Liengme" wrote:
>> >
>> >> In the SortTest sub use
>> >> If StrComp(UCase(KeyAy(Ix)), UCase(KeyAy(Jx)), CompareHow) = 1 Then
>> >>
>> >> Note the use of UCASE
>> >> But A, B, a. B in A1:A4; In B1 enter =CODE(A1);copu down the column.
>> >> Now
>> >> do
>> >> you see why the sort works the way it does?
>> >> best wishes
>> >> --
>> >> Bernard Liengme
>> >> Microsoft Excel MVP
>> >> http://people.stfx.ca/bliengme
>> >>
>> >> "Neal Zimm" <(E-Mail Removed)> wrote in message
>> >> news:46D21FA6-1BA7-482C-88E6-(E-Mail Removed)...
>> >> > 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
>> >>
>> >> .
>> >>

>>
>>
>> .
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with Bubble Sort on 2D Array =?Utf-8?B?RXhjZWxNb25rZXk=?= Microsoft Excel Programming 11 20th Feb 2006 04:50 PM
Misbehaving Bubble Sort Chris Microsoft VB .NET 4 23rd Nov 2005 12:19 AM
Calling Bubble Sort Function terryspencer2003@yahoo.ca Microsoft Excel Programming 6 26th Dec 2003 07:27 PM
need a code sample for bubble sort Mark Kamoski Microsoft C# .NET 34 10th Oct 2003 02:34 PM
Re: Bubble Sort Help Chris Dunaway Microsoft VB .NET 0 20th Aug 2003 10:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:39 PM.