Sorting Jagged Arrays

flyfisher65

New Member
Joined
Aug 7, 2011
Messages
3
language: VBA
I am trying to create a vba macro that will sort a jagged array (an array containing other arrays) by the elements of the first array. I want to keep the move the rest of the data in the "smaller" arrays with the column being sorted. This process is looped several times. Im not sure exactly how to describe it, but I tried to visualize it in the example below. I am not familiar with sorting code and would like some help on what to do. I tried putting it into an new worksheet and sorting it in excell, but some of the outputs (there are several because of the loop) were increasing L-R while others were deceasing. I want them to increase but am not sure how to do this.
Example (each character represents and element in the jagged array () encloses one of the "smaller" arrays)
(1AAAAA)
(4DDDDD)
(3CCCCC)
(2BBBBB)
would sort to
(1AAAAA)
(2BBBBB)
(3CCCCC)
(4DDDDD)
problem:
some outputs end up like this
(4DDDDD)
(3CCCCC)
(2BBBBB)
(1AAAAA)
thanks in advance
-Lewis
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
hi Lewis,

do you mean something like this?
Code:
Sub sortof()
Range("A1").CurrentRegion.sort Range("A1"), xlAscending, Header:=xlNo
End Sub
 
Upvote 0
Code:
Sub Array_Sort()
    
    Dim v As Variant, vTemp As Variant
    Dim i As Long, j As Long
    Dim col As Long
    
    v = Array(Array(1, "A", "A", "A", "A", "A"), _
              Array(4, "D", "D", "D", "D", "D"), _
              Array(3, "C", "C", "C", "C", "C"), _
              Array(2, "B", "B", "B", "B", "B"))
    'Sort
    col = 0 'Sort on column 0
    For i = LBound(v, 1) To UBound(v, 1) - 1
        For j = LBound(v, 1) To UBound(v, 1) - 1
            If v(j)(col) > v(j + 1)(col) Then
               vTemp = v(j)
               v(j) = v(j + 1)
               v(j + 1) = vTemp
            End If
    Next j, i
    
End Sub
 
Last edited:
Upvote 0
yes, the "xlAscending" option was what I was looking for. I realize now what a simple thing it was. Thanks for the help
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,144
Members
452,891
Latest member
JUSTOUTOFMYREACH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top