Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strComp function??

Status
Not open for further replies.

Nuqe

Technical User
May 6, 2003
57
CA
Hey i'm doing a sort using the strComp function... but i'm not getting what i need. I'm sorting from a higher value to a lower one. i'm taking values (integers) out of a database and putting them into a string(array). This is an example of what i'm getting...

69
65
64
109
108
107

this is my code

Do
intsflag = 0
For intx = 1 To UBound(strEntry, 2) - 1
r = StrComp(Val(strEntry(3, intx)), Val(strEntry(3, (intx + 1))), 0)

If r = -1 Then
For inti = 1 To 4
strsort(inti, 1) = strEntry(inti, (intx + 1))
strsort(inti, 2) = strEntry(inti, intx)
Next inti

For inti = 1 To 4
strEntry(inti, intx) = strsort(inti, 1)
strEntry(inti, (intx + 1)) = strsort(inti, 2)
Next inti
intsflag = 1
End If

Next intx

Loop While intsflag = 1

do i need another way of comparing?? any help would be great, thanks

NUQE

We all know about the "stupid user" don't we... :)
 
I suggest you to use the following routine:
Usage:
' Load strEntry
' then:
BubbleSort strEntry(),1,"ASC",1,Ubound(strEntry)
You can keep it as a general sort routine for arrays that are not too big, let's say 100. For larger arrays I would suggest you to use ShellSort or QuickSort

Public Function BubbleSort(SortArray As Variant, _
ArrDim As Variant, _
SortType As Variant, _
Optional SortCol As Variant, _
iLow As Integer, iHigh As Integer) As Variant
Dim iLow, iHigh, iLoop1, iLoop2, iLoop3 As Integer
Dim lTemp As Variant
' iLow = lower index to include in the sort
' iHigh = higher index to include in the sort
' ArrDim = number of dimension of the array
' SortType = "ASC" or "DSC" (ascending/descending )

If ArrDim = 1 Then
For iLoop1 = iHigh To iLow Step -1
For iLoop2 = iLow + 1 To iLoop1
If (SortType = "ASC" And SortArray(iLoop2 - 1) > SortArray(iLoop2)) Or _
(SortType = &quot;DSC&quot; And SortArray(iLoop2 - 1) < SortArray(iLoop2)) Then
lTemp = SortArray(iLoop2 - 1)
SortArray(iLoop2 - 1) = SortArray(iLoop2)
SortArray(iLoop2) = lTemp
End If
Next iLoop2
Next iLoop1
ElseIf ArrDim = 2 Then
For iLoop1 = iHigh To iLow Step -1
For iLoop2 = iLow + 1 To iLoop1
If (SortType = &quot;ASC&quot; And SortArray(iLoop2 - 1, SortCol) > SortArray(iLoop2, SortCol)) Or _
(SortType = &quot;DSC&quot; And SortArray(iLoop2 - 1, SortCol) < SortArray(iLoop2, SortCol)) Then
For iLoop3 = 0 To UBound(SortArray, 2)
lTemp = SortArray(iLoop2 - 1, iLoop3)
SortArray(iLoop2 - 1, iLoop3) = SortArray(iLoop2, iLoop3)
SortArray(iLoop2, iLoop3) = lTemp
Next iLoop3
End If
Next iLoop2
Next iLoop1
end if
BubbleSort = SortArray
End Function


I hope this help you,

Regards,
OBC
 
Yeah, it looks like you are getting the ASCII based sort order, which is what your code will do for you. In that case, it sorts numbers the same way it sorts words: the fact that there are less digits isn't taken into account, any more than Sid would come before Bobby because it has less letters.

You need to do your comparison on the Val part, not compare the strings. Seems obrain has already worked out the code and then some.

Bob
 
Thanks, that's a wack load of code compared to mine hehe i'll give it a shot and let ya know how it works. I'm sure it'll be alright.

Thanks again

NUQE

We all know about the &quot;stupid user&quot; don't we... :)
 
I just have a question the function bubblesort, is that a function you created, or is it an existing one?? I know nothing about functions, so that's why i ask. also if you have a link to where i can get more info on using functions that would be great. does this shell sort and quick sort work faster than the bubblesort??

anyways i got my sort working... thanks
Cheers
NUQE

We all know about the &quot;stupid user&quot; don't we... :)
 
In fact it is a function based on a well known algorithm, as QuickSort and ShellSort. What you got is an implementation that does Ascending and descending depending on a parameter, on the other hand it allows to sort only part of the array which could be handy in some cases. It also work as a full sort and as a tag sort.
The BubbleSort is preferred with an small number of elements to sort. The QuickSort can be up to 1,000 times faster when you go to large arrays, but uses recursivity, which many people try to avoid. The ShellSort is also very fast maybe around 800 times faster than BubbleSort, when working with large arrays, and it does not uses recursivity.
I am glad you got your Sort working, but I think you should try a faster algorithm.

Regards,
 
Alright thanks. I'll give these other methods a shot if i can figure them out. I sure could have used them in an earlier program when my database was bigger. This one, i'm probably not going to be sorting much more then 50 records. can i get examples of bubble,quick and shell sort in the msdn?

thanks again

NUQE

We all know about the &quot;stupid user&quot; don't we... :)
 
Thread222-500319

Sub ShellSortNumbers(vArray As Variant)
Dim lLoop1 As Long
Dim lHold As Long
Dim lHValue As Long
Dim lTemp As Long

lHValue = LBound(vArray)
Do
lHValue = 3 * lHValue + 1
Loop Until lHValue > UBound(vArray)
Do
lHValue = lHValue / 3
For lLoop1 = lHValue + LBound(vArray) To UBound(vArray)
lTemp = vArray(lLoop1)
lHold = lLoop1
Do While vArray(lHold - lHValue) > lTemp
vArray(lHold) = vArray(lHold - lHValue)
lHold = lHold - lHValue
If lHold < lHValue Then Exit Do
Loop
vArray(lHold) = lTemp
Next lLoop1
Loop Until lHValue = LBound(vArray)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top