Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sort Array
Message
From
23/07/2001 10:09:49
 
 
To
20/07/2001 14:20:00
Dave Sonier
Technologies Nter inc.
Gatineau, Quebec, Canada
General information
Forum:
Visual Basic
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
00533289
Message ID:
00533890
Views:
13
>Hi,
>
>I want to sort an array...Does exist a function in VB or does somebody have a code to do that...
>
>Thank in advance...
>
>Dave Sonier
>Technologies Nter
Here is the code I use it is a very slightly modified from the origional version from Brian Cidern it is rather blunt, but extremely fast :) I have tested this on very large arrays and it is faster than any other method I have seen. 3 seconds to sort 2000 50 character rows. Were the previous method took 29 seconds.
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cbCopy As Long)

Public Function foo()
    
    Dim xx(4) As String
    xx(0) = "fred"
    xx(1) = "dino"
    xx(2) = "Wilma"
    xx(3) = "barney"
    xx(4) = "Alex"
    
    Debug.Print " "
    Debug.Print "Before sort"
    Debug.Print "1 "; xx(0)
    Debug.Print "2 "; xx(1)
    Debug.Print "3 "; xx(2)
    Debug.Print "4 "; xx(3)
    Debug.Print "5 "; xx(4)
    Debug.Print " "
    
    SortMe xx

    Debug.Print "after sort"
    Debug.Print "1 "; xx(0)
    Debug.Print "2 "; xx(1)
    Debug.Print "3 "; xx(2)
    Debug.Print "4 "; xx(3)
    Debug.Print "5 "; xx(4)

End Function

Sub SortMe(varArray() As String)
    Dim i As Long, j As Long
    Dim l_Count As Long
    Dim l_Hold As Long
    
    ' Typical sorting routine
    l_Count = UBound(varArray)

    For i = 0 To l_Count

        For j = i + 1 To l_Count

            If varArray(i) > varArray(j) Then
                ' Here's the juice!
                SwapStrings varArray(i), varArray(j)
            End If
        Next
    Next
End Sub

Sub SwapStrings(pbString1 As String, pbString2 As String)
    Dim l_Hold As Long
    CopyMemory l_Hold, ByVal VarPtr(pbString1), 4
    CopyMemory ByVal VarPtr(pbString1), ByVal VarPtr(pbString2), 4
    CopyMemory ByVal VarPtr(pbString2), l_Hold, 4
End Sub
~Joe Johnston USA

"If ye love wealth better than liberty, the tranquility of servitude better than the animated contest of freedom, go home from us in peace. We ask not your counsel or arms. Crouch down and lick the hands which feed you. May your chains set lightly upon you, and may posterity forget that ye were our countrymen."
~Samuel Adams

Previous
Next
Reply
Map
View

Click here to load this message in the networking platform