Multi Dimentional Array Sort

# Thread: Multi Dimentional Array Sort

1. Junior Member
Join Date
Dec 1969
Posts
27

## Multi Dimentional Array Sort

I read article here about sort array, but how to sort multidimention array ?<BR><BR>I have an multidimentional array arrVB(5,4) (fixed lenght)<BR>Want to sort it acording to first element which is description and all other related also should get shifted in line with first element.<BR><BR>Btw, i want this only for display in table so any script - server side, fronend side should be ok for me<BR><BR>Thanks and regards<BR>Manisha

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## It's a pain in the rear...

...because in order to swap rows in the array, you have to swap *ALL* of the columns in each of the swapped rows.<BR><BR>It&#039;s better if you use an array *of* arrays (as you would have to do in JavaScript), because then swapping the rows is no different than with a 1D array.<BR><BR>BUT, since you asked:<BR><BR>First of all, I *assume* that you are using the VBScript/VB standard of <BR> cell = arrayName( columnNumber, rowNumber )<BR>which is backwards from most languages, but works better in VBS/VB because of the <BR> ReDim Preserve arrayName( columnCountCannotChange, anyRowCount )<BR><BR>So:<BR><BR>&#060;%<BR>Sub Sort2DArray( ByRef ar, sortColumn )<BR> Dim i, j, temp, col<BR> Dim rowMax = UBound(ar,2)<BR> Dim colMax = UBound(ar,1)<BR> For i = rowmax-1 To 0<BR> For j = 0 To i<BR> If ar(sortColumn,j) &#062; ar(sortColumn,j+1) Then<BR> &#039; swap<BR> For col = 0 To colMax<BR> temp = ar(col,j)<BR> ar(col,j) = ar(col,j+1)<BR> ar(col,j+1) = temp<BR> Next<BR> End If<BR> Next &#039; next j<BR> Next &#039; next i<BR>End Sub<BR>%&#062;<BR><BR>That&#039;s untested off the top of my head, but it feels right.<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Oops...sorry...

THis line: <BR> For i = rowmax-1 To 0<BR><BR>needs to be<BR> For i = rowmax-1 To 0 Step -1<BR><BR>so as to go backwards.<BR><BR>And if you have your columns and rows reversed from the standard, hopefully you can see what has to be swapped around.<BR><BR>

4. Junior Member
Join Date
Dec 1969
Posts
27

## RE: Multi Dimentional Array Sort

Thanks, will try it out<BR><BR>manisha

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•