ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   sort routine puts in wrong order (https://www.excelbanter.com/excel-programming/417962-sort-routine-puts-wrong-order.html)

owlnevada

sort routine puts in wrong order
 
I am attempting to modify this sort routine(which I didn't create) to sort a
series of variable length string arrays using permit numbers correctly. The
numbers that appear sort like this: 01316, 01327, 01329, 02884, 02888,
02889, 13198, 13200, 13580, 1937, 1958, 2157, 2289, 2789, 2855, 5160, 5837,
7548, 7605, 7606, 8339, 8813.

I'd like them to sort in true numeric ascending order like this: 1937, 1958,
2157, 2289, 2789, 2855, 5160, 5837, 7548, 7605, 7606, 8339, 8813, 13198,
13200, 13580, (with the ones with leading zeros to follow like this:) 01316,
01327, 01329, 02884, 02888, 02889 . . . 09999

I've searched these posts and several sites and can't find a good fit, but
surely someone else may have encountered a similar sort problem. AM too new
to programming to savy all this code so maybe someone more experienced can
steer me or add to this.

Any help muchas gracias!




Private Sub QuickSort(strArray() As String, intBottom As Integer, intTop As
Integer)

Dim strPivot As String, strTemp As String
Dim intBottomTemp As Integer, intTopTemp As Integer

intBottomTemp = intBottom
intTopTemp = intTop

strPivot = strArray((intBottom + intTop) \ 2)

While (intBottomTemp <= intTopTemp)

While (strArray(intBottomTemp) < strPivot And intBottomTemp < intTop)
intBottomTemp = intBottomTemp + 1
Wend

While (strPivot < strArray(intTopTemp) And intTopTemp intBottom)
intTopTemp = intTopTemp - 1
Wend

If intBottomTemp < intTopTemp Then
strTemp = strArray(intBottomTemp)
strArray(intBottomTemp) = strArray(intTopTemp)
strArray(intTopTemp) = strTemp
End If

If intBottomTemp <= intTopTemp Then
intBottomTemp = intBottomTemp + 1
intTopTemp = intTopTemp - 1
End If

Wend

'the function calls itself until everything is in good order
If (intBottom < intTopTemp) Then QuickSort strArray, intBottom, intTopTemp
If (intBottomTemp < intTop) Then QuickSort strArray, intBottomTemp, intTop

End Sub

RB Smissaert

sort routine puts in wrong order
 
Try these changes:
Change strArray() As String to LongArray() As Long
Change strPivot As String to LongPivot As Long
While you are at it you might as well change all Integer to Long.

RBS


"owlnevada" wrote in message
...
I am attempting to modify this sort routine(which I didn't create) to sort
a
series of variable length string arrays using permit numbers correctly.
The
numbers that appear sort like this: 01316, 01327, 01329, 02884, 02888,
02889, 13198, 13200, 13580, 1937, 1958, 2157, 2289, 2789, 2855, 5160,
5837,
7548, 7605, 7606, 8339, 8813.

I'd like them to sort in true numeric ascending order like this: 1937,
1958,
2157, 2289, 2789, 2855, 5160, 5837, 7548, 7605, 7606, 8339, 8813, 13198,
13200, 13580, (with the ones with leading zeros to follow like this:)
01316,
01327, 01329, 02884, 02888, 02889 . . . 09999

I've searched these posts and several sites and can't find a good fit, but
surely someone else may have encountered a similar sort problem. AM too
new
to programming to savy all this code so maybe someone more experienced can
steer me or add to this.

Any help muchas gracias!




Private Sub QuickSort(strArray() As String, intBottom As Integer, intTop
As
Integer)

Dim strPivot As String, strTemp As String
Dim intBottomTemp As Integer, intTopTemp As Integer

intBottomTemp = intBottom
intTopTemp = intTop

strPivot = strArray((intBottom + intTop) \ 2)

While (intBottomTemp <= intTopTemp)

While (strArray(intBottomTemp) < strPivot And intBottomTemp < intTop)
intBottomTemp = intBottomTemp + 1
Wend

While (strPivot < strArray(intTopTemp) And intTopTemp intBottom)
intTopTemp = intTopTemp - 1
Wend

If intBottomTemp < intTopTemp Then
strTemp = strArray(intBottomTemp)
strArray(intBottomTemp) = strArray(intTopTemp)
strArray(intTopTemp) = strTemp
End If

If intBottomTemp <= intTopTemp Then
intBottomTemp = intBottomTemp + 1
intTopTemp = intTopTemp - 1
End If

Wend

'the function calls itself until everything is in good order
If (intBottom < intTopTemp) Then QuickSort strArray, intBottom,
intTopTemp
If (intBottomTemp < intTop) Then QuickSort strArray, intBottomTemp,
intTop

End Sub




All times are GMT +1. The time now is 11:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com