About asort

About asort

Postby Marco Turco » Mon Sep 26, 2011 3:24 pm

Hi all,
I need to order a multidimensional array with two criterias.
Is there a way to make an order this I use for a dbf sort on lb02/a,lb11/a to &filelb on an Array using asort ?

Thanks in advance.
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: About asort

Postby James Bott » Mon Sep 26, 2011 3:30 pm

Marco,

Maybe this old message will help.

James

----------------------------------------
From: "Victor Spiridonov" <dicch@mail.ru>
Subject: Re: Sorting multidimensional array
Date: Wednesday, November 05, 2003 11:06 PM

"Stephen Quinn" <steveqNOSPAM@integritynet.com.au> wrote in message news:<bobukt$1d6h8g$1@ID-88745.news.uni-berlin.de>...
> decray
>
> > I'm trying to sort multidimensional arrays.
> > I have tried with asort(aarray,,,{|x,y| x[1] >y[1]}); but this only
> > change the column order and it doesnt work.
> 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

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)

Code: Select all  Expand view
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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: About asort

Postby Carlos Mora » Mon Sep 26, 2011 4:38 pm

Hello Marco,
Marco Turco wrote:Hi all,
I need to order a multidimensional array with two criterias.
Is there a way to make an order this I use for a dbf sort on lb02/a,lb11/a to &filelb on an Array using asort ?


ASORT(<aTarget>, [<nStart>],[<nCount>], [<bOrder>]) --> aTarget

The trick is made by bOrder. It recives 2 row like arrays , so you have to compare first column and IF IT DOESNT SOLVES ORDER (The fisrt compared element are equal) then solve the comparision with the second.

Said in code:

Code: Select all  Expand view
aSort( aArray,,,{|x,y| If( x[1] == y[1], x[2]<y[2], x[1]<y[1] ) } )


you should adjust the indexes to match your array columns.

Hope it helps
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
Carlos Mora
 
Posts: 988
Joined: Thu Nov 24, 2005 3:01 pm
Location: Madrid, España

Re: About asort

Postby Marco Turco » Tue Sep 27, 2011 3:03 pm

It runs well. Thank you Carlos.
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 33 guests