|
-
Aug 13th, 2001, 05:37 PM
#1
Thread Starter
Fanatic Member
sorting
whats the most efficient way to sort a bunch of fields, like in excel if you have several columns of data you can tell it to sort by column A then by column B and so on
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Aug 13th, 2001, 11:50 PM
#2
If you're using Excel, why not create a macro, sort the fields usig the menu then stop the macro & pinch the code created ?
-
Aug 14th, 2001, 06:20 AM
#3
Member
Code:
Dim rsSource As Recordset
Dim rsTarget As Recordset
'Get rsSource
rsSource.Sort = "Name"
Set rsTarget = rsSource.OpenRecordset
I suggest to keep rsSource for future sortings.
Otherwise, you can also directly
Set rsSource = rsSource.OpenRecordset
Hope that helps.
Felix
-
Aug 14th, 2001, 11:32 AM
#4
Thread Starter
Fanatic Member
problem is im not using excel and im not using databases (and i don't wanna learn databases right now), im just using a couple class modules with variables and i want to write an algorithm to sort them the good ol' fashioned way. i just don't know how
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Aug 14th, 2001, 12:29 PM
#5
Lively Member
Hi, i use normaly the quicksort algorythm for sorting items in a listbox or so.
You have to determine where in the string your fields start and how long they are.
Here is the code
Code:
' Sort the items in array values() with bounds min and max.
Sub Quicksort(values() As String, _
ByVal SortStart As Integer, _
ByVal SortLen As Integer, _
ByVal min As Long, _
ByVal max As Long)
Dim med_value As String
Dim hi As Long
Dim lo As Long
Dim i As Long
On Error Resume Next
' If the list has only 1 item, it's sorted.
If min >= max Then Exit Sub
' Pick a dividing item randomly.
i = min + Int(Rnd(max - min + 1))
med_value = values(i)
' Swap the dividing item to the front of the list.
values(i) = values(min)
' Separate the list into sublists.
lo = min
hi = max
Do
' Look down from hi for a value < med_value.
Do While Mid$(values(hi), SortStart, SortLen) >= _
Mid$(med_value, SortStart, SortLen)
hi = hi - 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
' The list is separated.
values(lo) = med_value
Exit Do
End If
' Swap the lo and hi values.
values(lo) = values(hi)
' Look up from lo for a value >= med_value.
lo = lo + 1
Do While Mid$(values(lo), SortStart, SortLen) < _
Mid$(med_value, SortStart, SortLen)
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
' The list is separated.
lo = hi
values(hi) = med_value
Exit Do
End If
' Swap the lo and hi values.
values(hi) = values(lo)
Loop ' Loop until the list is separated.
' Recursively sort the sublists.
Quicksort values, SortStart, SortLen, min, lo - 1
Quicksort values, SortStart, SortLen, lo + 1, max
End Sub
The goal of Software Engineering is to build something that will last at least until we've finished building it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|