UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Sort Array?    
 
   
ry94080
post Jan 8 2009, 03:17 PM
Post#1



Posts: 924
Joined: 27-July 05



Hello Access users,
I'm trying to sort a numeric array from lowest to highest... Any ideas?
Thanks,
Ryan
Go to the top of the page
 
doctor9
post Jan 8 2009, 04:11 PM
Post#2


UtterAccess Editor
Posts: 17,924
Joined: 29-March 05
From: Wisconsin


Ryan,
There are a bunch of ways to sort an array. Here's one I copied out of a Microsoft demo database.
CODE
  
Sub ShellSort(a() As Integer)
'
' Very fast sort: 2n Log n comparisons
'
' Calling convention:
'   Redim A(1 To 20) as Integer
'   ShellSort A()
'
Dim i As Integer, j As Integer, Low As Integer, Hi As Integer
Dim Temp As Integer, Swapped As Integer
  Low = LBound(a)
  Hi = UBound(a)
  j = (Hi - Low + 1) \ 2
  Do While j > 0
    For i = Low To Hi - j
      If a(i) > a(i + j) Then
        Temp = a(i)
        a(i) = a(i + j)
        a(i + j) = Temp
      End If
    Next i
    For i = Hi - j To Low Step -1
      If a(i) > a(i + j) Then
        Temp = a(i)
        a(i) = a(i + j)
        a(i + j) = Temp
      End If
    Next i
    j = j \ 2
  Loop
End Sub

Hope this helps,
Dennis
Go to the top of the page
 
datAdrenaline
post Jan 8 2009, 04:14 PM
Post#3


UtterAccess Editor
Posts: 17,941
Joined: 4-December 03
From: Northern Virginia, USA


Yep ...
!--c1-->
CODE
Public Sub SortArray(ByRef varArray As Variant)
'Sort an array of values.  This sub does not return anything, since
'it sorts the array in the same memory space that it occupies.
    
    Dim x As Long
    Dim varTemp As Variant
    Dim lngElement As Long
    
    'Test to see if it is an array, if not, then exit
    If Not IsArray(varArray) Then
        Exit Sub
    End If
    
    'Bubble sort the array
    If UBound(varArray) > 0 Then
        x = UBound(varArray)
        Do Until x = 0
            For lngElement = LBound(varArray) To x - 1
                If varArray(lngElement) > varArray(lngElement + 1) Then
                    varTemp = varArray(lngElement)
                    varArray(lngElement) = varArray(lngElement + 1)
                    varArray(lngElement + 1) = varTemp
                End If
            Next lngElement
            x = x - 1
        Loop
    End If
  
End Sub

To use this ... you PASS the Array variable name to it, the procedure will sort the Array "in place" since Array's are passed "ByRef" ...
....
Also .. just as an FYI .. if you have a String array you need sorted, you can use a Hidden function ....
WizHook.SortStringArray
But, the SortArray() code I posted will sort string arrays too ...
Go to the top of the page
 
PeterDoering
post Jan 9 2009, 04:12 AM
Post#4



Posts: 17
Joined: 27-October 08



I have another one:
CODE
  
Sub SortArr(ByRef vArray() As Variant)
    
  Dim I As Long
  Dim J As Long
  Dim vWrk As Variant
  
  If Not IsArray(vArray) Then Exit Sub
  
  For I = LBound(vArray) To UBound(vArray) - 1
    For J = I + 1 To UBound(vArray)
      If vArray(I) > vArray(J) Then
        vWrk = vArray(J)
        vArray(J) = vArray(I)
        vArray(I) = vWrk
      End If
    Next J
  Next I
    
End Sub

To test try that:
CODE

Sub TestArr()
    
    Dim MyArray() As Variant
    
    'Alpha-num Array
    MyArray = Array("Ryan", "Dennis", "Brent", "Peter")
    
    SortArr MyArray
        
    For I = LBound(MyArray) To UBound(MyArray)
        Debug.Print MyArray(I)
    Next I
    
    'Numeric Array
    MyArray = Array(2, 1, 5, 3, 7, 4)
    
    SortArr MyArray
        
    For I = LBound(MyArray) To UBound(MyArray)
        Debug.Print MyArray(I)
    Next I
    
    
End Sub

Rgds - Peter
Go to the top of the page
 
mishej
post Jan 9 2009, 04:57 AM
Post#5


Retired Moderator
Posts: 11,289
Joined: 25-September 02
From: Milwaukee, WI


There is a great sample database available here:
http://www.groupacg.com/files/sorts01.zip
Sequence Dancing - it is A97 but it will work. Open it in A2007 and agree to convert it. Ignore the "Conversion Errors" table.
Open the code editor (Alt-F11), select Tools, References and un-click the reference marked MISSING (Utility97.mda).
Close, and select Debug, Compile and then File, Save. Then select Office Button, Manage, Compact and Repair.
Hopefully that works for you. Nice sample, just a little dated. This is more of a research project than a direct answer so consider that before downloading. But it let's you compare the textbox sort algorithms. FWIW...
Go to the top of the page
 
ry94080
post Jan 9 2009, 11:04 AM
Post#6



Posts: 924
Joined: 27-July 05



Thanks for all the suggestion guys, I randomly chose to use datAdrenaline, seems to work fine.
Thanks again.
uarulez2.gif
Go to the top of the page
 
ry94080
post Jan 9 2009, 12:41 PM
Post#7



Posts: 924
Joined: 27-July 05



I'm very close, but i think i may have a datatype issue when i'm splitting a string with a delimitor to an array.
Can somebody help with what i'm doing wrong below???
CODE

Function PassArray(sRuleStamps)
Dim vArray As Variant
vArray = Split(sRuleStamps, ",")
PassArray = SortArray(vArray)
End Function

Function SortArray(ByRef varArray As Variant)
'Sort an array of values.  This sub does not return anything, since
'it sorts the array in the same memory space that it occupies.
    
    Dim x As Long
    Dim varTemp As Variant
    Dim lngElement As Long
    
    'Test to see if it is an array, if not, then exit
    If Not IsArray(varArray) Then
        Exit Function
    End If
    
    'Bubble sort the array
    If UBound(varArray) > 0 Then
        x = UBound(varArray)
        Do Until x = 0
            For lngElement = LBound(varArray) To x - 1
                If varArray(lngElement) > varArray(lngElement + 1) Then
                    varTemp = varArray(lngElement)
                    varArray(lngElement) = varArray(lngElement + 1)
                    varArray(lngElement + 1) = varTemp
                End If
            Next lngElement
            x = x - 1
        Loop
    End If
SortArray = varArray(1)
End Function
Go to the top of the page
 
datAdrenaline
post Jan 9 2009, 02:05 PM
Post#8


UtterAccess Editor
Posts: 17,941
Joined: 4-December 03
From: Northern Virginia, USA


What did you expect PassArray to return? ...
emember ... SortArray() does not RETURN anything (its NOT a Function ... its a Sub, so it does not belong on the right side of an "=") ... it sorts the Array "in place" ... Also, do take note that Split() populates an array of STRINGs ...
To illustrate, lets modify PassArray a bit and return the sorted array as a CSV ...
CODE
Function PassArray() As String
    
    Dim sRuleStamps As String
    Dim vArray As Variant
    
    sRuleStamps = "10,9,8,7,6,5,4,3,2,1,0"
    vArray = Split(sRuleStamps, ",")
    
    SortArray vArray
    
    PassArray = Join(vArray, ",")
    
End Function

If ran in the Immediate window you get ...
? PassArray
0,1,10,2,3,4,5,6,7,8,9
{Remember Split() populates a String array, so the Sorting is AlphaNumeric, that is why the 10 comes after the 1}
Now ... lets make it a numeric Array ...
CODE
Function PassArray() As String
    
    Dim vArray As Variant
    
    vArray = Array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
    
    SortArray vArray
    
    PassArray = Join(vArray, ",")
    
End Function

Now you get ...
? PassArray
0,1,2,3,4,5,6,7,8,9,10
.....
Does that make sense?
Go to the top of the page
 
ry94080
post Jan 9 2009, 03:08 PM
Post#9



Posts: 924
Joined: 27-July 05



What i really need to return is the lowest number out of the array.
On a nutshell what i'm doing is looking for these strings in a variable. "R12SW", "RR4SW", "R22SW", "R13SW"
They can occur in any portion in the string, what I need to find is the first occurance of either of the strings above. When found, I can parse it which is a different story.
What i'm doing is looking for either of these strings, with the InStr function, which will return where it's found those strings and i put those values in an array for example: (113, 178, 89). What I need to do is get the lowest value(which is the first occurance).
Am i doing something wrong?
Go to the top of the page
 
datAdrenaline
post Jan 9 2009, 05:18 PM
Post#10


UtterAccess Editor
Posts: 17,941
Joined: 4-December 03
From: Northern Virginia, USA


Ok ... then, you really don't need to sort an array ... just keep track of the value of the position you find in your long string and if the position of the next find is > 0 and < the lowest one previously found, then adjust your low number ...
Here is some Air Code ....
CODE
Public Function FindStartPosition(strSomeLongString As String) As Long
    
    Const cKeyValues As String = "R12SW,RR4SW,R22SW,R13SW"
    
    Dim aKeyValue() As String
    Dim lngTemp As Long
    Dim lngPosition As Long
    Dim x As Integer
    
    'Split your search keys
    aKeyValue = Split(cKeyValues, ",")
    
    'Look for the key that is closest to the start of the passed string
    For x = LBound(aKeyValue) To UBound(aKeyValue)
        
        lngTemp = InStr(1, strSomeLongString, aKeyValue(x), vbTextCompare)
        
        If lngTemp > 0 Then _
            If lngTemp < lngPosition Or lngPosition = 0 _
                Then lngPosition = lngTemp
                
    Next x
    
    'Return the value
    FindStartPosition = lngPosition
    
End Function
Go to the top of the page
 
ry94080
post Jan 9 2009, 05:33 PM
Post#11



Posts: 924
Joined: 27-July 05



Good lord... That logic seem so much more simpler. I'm sure that'll work. I'll apply and get back to you.
Thank you datAdrenaline
o!
Go to the top of the page
 
datAdrenaline
post Jan 11 2009, 05:10 PM
Post#12


UtterAccess Editor
Posts: 17,941
Joined: 4-December 03
From: Northern Virginia, USA


You're welcome ... thumbup.gif
Go to the top of the page
 
ry94080
post Jan 14 2009, 01:37 PM
Post#13



Posts: 924
Joined: 27-July 05



Worked like a charm, thanks again.
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    16th December 2017 - 01:33 PM