Results 1 to 5 of 5

Thread: sorting

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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.

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    If you're using Excel, why not create a macro, sort the fields usig the menu then stop the macro & pinch the code created ?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    Member
    Join Date
    Sep 1999
    Location
    Zurich, Switzerland
    Posts
    55
    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

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    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.

  5. #5
    Lively Member knochenfish's Avatar
    Join Date
    Apr 2001
    Location
    Cologne
    Posts
    77
    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
  •  



Click Here to Expand Forum to Full Width