Bubble Sort Within The Same Field (1 Viewer)

hbtousa

Registered User.
Local time
Yesterday, 19:12
Joined
Jan 25, 2007
Messages
14
Hello. I am trying to sort every line of a table; the table has 1000+records with data as follows:
LA60RH90RW23LE9LS10RE12RA140LH2RS5RK780LW2LK7
Every line has TWO letters and THREE "digits" twelves times.
I would like to sort every line in ascending order. For example the end result would be LW2LH2RS5LK7LE9LS10RE12RW23LA60RH90RA140RK780 .
I tried the bubble sort using a different programming language and it worked.However, I need to use Access since the amount of data is pretty big. Any help would be highly appreciate it, and just for the record I am a novice programmer and I don't how or where to start. Thanks a lot
The function I used and worked is described below:
Code:
void sort(int Numbers[], int size) {
  for (int First = 0; First < (size - 1); First++) {
    for (int Inside = 0; Inside < (size - (First + 1)); Inside++) {
      if (Numbers[Inside] > Numbers[Inside + 1]) {
        int Stored = Numbers[Inside];
        char *Temp = Parts[Inside];
        Parts[Inside] = Parts[Inside + 1];
        Numbers[Inside] = Numbers[Inside + 1];
        Numbers[Inside + 1] = Stored;
        Parts[Inside + 1] = Temp;        
        }
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 19:12
Joined
Jan 23, 2006
Messages
15,378
When you cross post --same or similar post on multiple forums without notification of those other posts -- you will turn readers off. You should identify the other posts and forums.
Here is why.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:12
Joined
Feb 28, 2001
Messages
27,167
Nobody does a Bubble Sort in Access unless it is a school project. In a regular business, a programmed Bubble Sort is a waste of resources. We just use SQL with an ORDER BY verb. (Access will use a B-tree style of sort.)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:12
Joined
May 7, 2009
Messages
19,230
add this function in a Module in VBA:
Code:
Public Function fnSpecialSort(ByVal strToCheck As String) As String
    Dim m As Variant
    Dim Match As Variant
    Dim Re As Object
    Dim rslt As Variant
    Dim a() As String
    Dim i As Integer
    'create regular expression object
    Set Re = CreateObject("VBScript.RegExp")
    Re.Global = True
    Re.IgnoreCase = True
    'pattern for AA99999
    Re.pattern = "[a-zA-Z]{1,100}[0-9]{1,100}"
    Set Match = Re.Execute(strToCheck)
    i = 0
    For Each m In Match
        i = i + 1
        ReDim Preserve a(0 To i)
        a(i) = m.value
    Next
    If ArrayHasElement(a) Then
        'get the last number from the string
        Re.pattern = "(\d+)"
        For i = 1 To UBound(a)
            Set Match = Re.Execute(a(i))
            For Each m In Match
                'move the number portion in front and format it in "00000"
                a(i) = Format(m, "00000") & Replace(a(i), m, "")
            Next
        Next
        'sort the array
        Call QuickSort(a)
        'reinstate the number to the end of the string
        For i = 1 To UBound(a)
            a(i) = Mid(a(i), 6) & Val(a(i))
        Next
        'assemble the array into string
        fnSpecialSort = Join(a, "")
    End If
    Set m = Nothing
    Set Re = Nothing
End Function

''''''''''''''''''''
' helper function for fnSpecialSort() function
''''''''''''''''''''
Private Function ArrayHasElement(a As Variant) As Boolean
    On Error Resume Next
    Dim var As Variant
    var = a(0)
    ArrayHasElement = (Err.Number = 0)
    Err.Clear
    On Error GoTo 0
End Function

Public Sub QuickSort(ByRef aSortArray As Variant, Optional ByVal lngFirst As Long = -1, Optional ByVal lngLast As Long = -1)
'Recursive array quicksort function single dimension

    Dim lngLow  As Long, lngHigh As Long
    Dim vntTemp As Variant, vntList_Separator As Variant

    lngFirst = IIf(lngFirst < 0, LBound(aSortArray), lngFirst)
    lngLast = IIf(lngLast < 0, UBound(aSortArray), lngLast)
    lngLast = IIf(lngLast > UBound(aSortArray), UBound(aSortArray), lngLast)
    lngLow = lngFirst
    lngHigh = lngLast

    vntList_Separator = aSortArray((lngFirst + lngLast) / 2)
    Do
        Do While (aSortArray(lngLow) < vntList_Separator)
            lngLow = lngLow + 1
        Loop
        Do While (aSortArray(lngHigh) > vntList_Separator)
            lngHigh = lngHigh - 1
        Loop
        If (lngLow <= lngHigh) Then
            vntTemp = aSortArray(lngLow)
            aSortArray(lngLow) = aSortArray(lngHigh)
            aSortArray(lngHigh) = vntTemp
            lngLow = lngLow + 1
            lngHigh = lngHigh - 1
        End If
    Loop While (lngLow <= lngHigh)
    If (lngFirst < lngHigh) Then QuickSort aSortArray, lngFirst, lngHigh
    If (lngLow < lngLast) Then QuickSort aSortArray, lngLow, lngLast

End Sub

example:
Code:
dim sStringToSort As String
dim sSortedString As String
sStringToSort = "LA60RH90RW23LE9LS10RE12RA140LH2RS5RK780LW2LK7"
sSortedString = fnSpecialSort(sStringToSort)
Debug.Print sSortedString

'return value is
LH2LW2RS5LK7LE9LS10RE12RW23LA60RH90RA140RK780
 

Users who are viewing this thread

Top Bottom