Results 1 to 5 of 5

Thread: [RESOLVED] How do I LexicoGraphically Sort a Array of Arrays in vb6

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    129

    Resolved [RESOLVED] How do I LexicoGraphically Sort a Array of Arrays in vb6

    Hello,

    What i'm trying to do is sort lexicographically (abc order, well in my case in incremental byte order).
    I got very confused when I attempted to do this myself.

    This is the code how I attempted its a complete disaster.
    First of all it has process the array of bytes as a multidimensional array even though it's just 1 array.
    Secondly it has to pick the first byte of each row in the array I assume sort all that in a increasing number.
    Save the indexes of the already sorted first byte of each row.
    Then do this to the second byte in the first row keeping in mind that if a match actually occurs it also has to remain sorted most importantly using the first byte of each row. Very confusing task thats why I ask for help.


    Here is a example of how it suppose to work say you got this data.
    6 4 4 2 9 3 1 7 4 1 3 3 9 4 1 3 3 8 3 1

    the data will has to be represented by a square because its a array of arrays
    so like this
    6 4 4 2 9
    3 1 7 4 1
    3 3 9 4 1
    3 3 8 3 1

    Then the lexico sort should result in a list that looks like this

    1,3,2,0

    because if you sort it properly it should look like this

    3 1 7 4 1
    3 3 8 3 1
    3 3 9 4 1
    6 4 4 2 9

    Code:
    Function SortArrayOfArraysLexicoGraphically(data() As Byte) As Byte()
    Dim lexicoGraphicalIndexes() As Byte
    
    Dim dataSize As Long
    dataSize = UBound(data) + 1
    Dim squareRoot As Byte
    sqaureRoot = Sqr(dataSize)
    ReDim lexicoGraphicalIndexes(sqaureRoot - 1)
    
    Dim j As Long
    Dim i As Long
    Dim isSorted As Boolean
    isSorted = False
    
    Do While Not isSorted
        isSorted = True
        j = 0
        For i = 0 To UBound(data) Step sqaureRoot 'This loops by columns down
            If i = (dataSize - sqaureRoot) Then 'last row
                Exit For
            End If
            
            'If first row, first byte > next row, first byte then
            If data(i) > data(i + sqaureRoot) Then
                lexicoGraphicalIndexes(j + 1) = j
                lexicoGraphicalIndexes(j) = j + 1
                i = i + sqaureRoot 'skip next one it's already sorted.
                j = j + 1
                isSorted = False
            ElseIf data(i) = data(i + sqaureRoot) Then
            
            Else
                lexicoGraphicalIndexes(j) = j
            End If
            j = j + 1
        Next i
    Loop
    
    'returns a byte array of sorted indexes.
    SortArrayOfArraysLexicoGraphically = lexicoGraphicalIndexes
    End Function
    Last edited by sspoke; Aug 25th, 2012 at 03:56 PM.

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