Sorting Arrays With More Than one Dimension

You need to reinitialize your array into a single dimension, sort it, and reinitialize it again.

With QB:

```
'Assume you sort from 0 to ubound(array).

'For array1(1 TO 20, 1 TO 25, 1 TO 10):
DIM array2(20*25*10-1)

'Code to "flatten" the array:
FOR i% = 1 TO 20
FOR j% = 1 TO 25
FOR k% = 1 TO 10
array2(i%-1 + (j%-1)*20 + (k%-1)*500) = array1(i%, j%, k%)

'Note the (i%-1), (j%-1), and (k%-1). Make sure your array2 starts with 0.
'
'IE:
'When all the values are minimal, the index should be 0.
'When they are maximum, the index should be the amount of total values.

NEXT k%, j%, i%

'Now sort it with a sorting routine.
qsort1 array2(), LBOUND(array2()), UBOUND(array2())

'Ok now reformulate the dimensions; it's just the opposite of what was previously done.
FOR i% = 1 TO 20
FOR j% = 1 TO 25
FOR k% = 1 TO 10
array1(i%, j%, k%) = array2(i%-1 + (j%-1)*20 + (k%-1)*500)
NEXT k%, j%, i%

```

-Agamemnus

SortingArraysWithMoreThanOneDimension - page last edited 2005-06-10 13:09:02 by 24.128.99.203 (home) (edit)
Blast WIKI - by RoboticBoy - edited and tweaked for our evil purposes by Hexadecimal Disaster