## Sorting multidimensional array

### Sorting multidimensional array

This is a multi-part message in MIME format.

...

Here's a class I wrote a long time ago with FiveWin to simulate a
one-dimension array of up to 4096*4096 elements by using an array of arrays.
Take a look at the sort method for one way of doing this.

--
Ray Marron

### Sorting multidimensional array

decray

It does - your just NOT getting the result you want <g>

If you want to sort each dimension then try sorting them individually

Eg - Untested
for i := 1 to len( aArray )
aArray1 := aArray[i]
asort( aArray1,,,{|x,y| x[1] >= y[1] } )
aArray[i] := aArray1
next

### Sorting multidimensional array

Decray
here is a function you are looking for
being distributed free with our demo
It's old enough and seems to be one of the fastest sorting

FUNCTION mFaSo2Dim(mAr,mColumn,mL,mR) // fast sorting of two-dimentional array mAr
// mL - index of the first element of area to be sorted
// mR - index of the last element of the area to sort
// mAr - array to be sorted
// mColumn - column array to be sorted on
//Function returns Nil

Local mSrtFlg,mI,mJ,mX,mW,mV,mT
if mL=nil; mL=1; endif
if mR=nil; mR=len(mAr); endif
if mR>1
mSrtFlg=.t.
mI=mL
mJ=mR
mX=mAr[(mL+mR)/2,mColumn]
Do While ((mI<mJ).or.mSrtFlg)
mSrtFlg = .f.
Do While (mAr[mI,mColumn]<mX)
mI++
EndDo
Do While (mAr[mJ,mColumn]>mX)
mJ--
EndDo
If mI <= mJ
mW = mAr[mI]
mAr[mI] = mAr[mJ]
mAr[mJ] = mW
mI++
mJ--
EndIf
Loop
EndDo
If mL < mJ
mQuSo2Dim(mL,mJ,mAr,mColumn)
EndIf
If mR > mI
mQuSo2Dim(mI,mR,mAr,mColumn)
EndIf
Endif
RETURN Nil

Regards,
Victor Spiridonov

### Sorting multidimensional array

Decray
Below is the function you are looking for
(in my previous message I've not replace 2 recursive calls to
the new function name and parameters order)

FUNCTION mFaSo2Dim(mAr,mColumn,mL,mR) // fast sorting of two-dimentional array mAr
// mAr - array to be sorted
// mColumn - column array to be sorted on
// mL - index of the first element of area to be sorted
// mR - index of the last element of the area to sort

//Function returns Nil

Local mSrtFlg,mI,mJ,mX,mW,mV,mT

if mL=nil; mL=1; endif
if mR=nil; mR=len(mAr); endif
if mR>1
mSrtFlg=.t.
mI=mL
mJ=mR
mX=mAr[(mL+mR)/2,mColumn]
Do While ((mI<mJ).or.mSrtFlg)
mSrtFlg = .f.
Do While (mAr[mI,mColumn]<mX)
mI++
EndDo
Do While (mAr[mJ,mColumn]>mX)
mJ--
EndDo
If mI <= mJ
mW = mAr[mI]
mAr[mI] = mAr[mJ]
mAr[mJ] = mW
mI++
mJ--
EndIf
Loop
EndDo
If mL < mJ
mFaSo2Dim(mAr,mColumn,mL,mR)
EndIf
If mR > mI
mFaSo2Dim(mAr,mColumn,mL,mR)
EndIf
Endif
RETURN Nil

### Sorting multidimensional array

You might want to include this function code too:

mFaSo2Dim(mAr,mColumn,mL,mR)

Mike