Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sorting multidim array

13 views
Skip to first unread message

Rajeev Chavan

unread,
Mar 2, 2004, 3:04:13 AM3/2/04
to
Hi All
Does anyone have a program to sort multidimensional array ?
TIA

--
Rajeev Chavan
email to : sschavan at eth dot net


Ray Marron

unread,
Mar 2, 2004, 9:47:46 AM3/2/04
to
"Rajeev Chavan" <sschavan...@eth.net> wrote in message
news:c21esr$1ne4b0$1...@ID-214544.news.uni-berlin.de...
...

> Does anyone have a program to sort multidimensional array ?

I have lots, but they're all different based upon what I'm sorting. If you
can be more specific, we can give a more specific answer.

I find that if you just keep in mind that multidimensional arrays are simply
"arrays of arrays", things go easier.

--
Ray Marron

Gejza Horvath

unread,
Mar 2, 2004, 2:13:53 PM3/2/04
to
Look on the ASORT() function in your NG document. It contains example, how
to sort two dimensional array.

Gejza

"Rajeev Chavan" <sschavan...@eth.net> píąe v diskusním příspěvku
news:c21esr$1ne4b0$1...@ID-214544.news.uni-berlin.de...

AUGE_OHR

unread,
Mar 2, 2004, 6:54:19 PM3/2/04
to
Example sorting Directory() multidimensional array :

#include "Directry.ch"
#include "Common.ch"
#include "dll.ch"

#xtranslate .filename => \[1\]
#xtranslate .filesize => \[2\]
#xtranslate .filedate => \[3\]
#xtranslate .filetime => \[4\]
#xtranslate .fileattrib => \[5\]

#xtranslate aDIZ => Stack\[SP,1]
#xtranslate nLEN => Stack\[SP,2]
#xtranslate nCount => Stack\[SP,3]
#xtranslate nStartAt => Stack\[SP,4]

STATIC Stack := {}
STATIC SP := 0
STATIC cDirStart := ""
STATIC aALLFILES := {} // if W98 may be 4096 Limit

PROCEDURE ST_INIT
AADD(Stack,ARRAY(4))
SP++
nStartAt := 1
RETURN

PROCEDURE ST_EXIT
Stack := ASIZE(Stack,--SP)
RETURN

PROCEDURE MAIN(cpar1,cpar2,cpar3,cpar4)
cDirStart := CURDRIVE()+":\"+CURDIR()+"\" // Starting Directory
SET ALTER TO DIREMPTY.BAT // just have a LOG File
SET ALTER ON
RECUDIRS(cDirStart,"*.*",{|x,y|DOWWORK(x,y)}) // ONLY Extension
SET ALTER OFF
SET ALTER TO
RETURN

PROCEDURE RECUDIRS(p_path,p_wildcard,bblock)

ST_INIT() // Init Stack

aDIZ := Directory(p_path+"*.*","D")
nLEN := LEN(aDIZ)

// make all FileNames LOWER()
AEVAL(aDIZ, {|x| x.filename := LOWER(x.filename) })
ASORT(aDIZ,,, {|x,y| x.filename < y.filename})

// First, put all sub-directories
// at the beginning of the list
ASORT(aDIZ,,, {|x,y| "D" $ x.fileattrib })

// Now, lets find out where the other
// files are
AEVAL(aDIZ, {|x,i| nStartAt := ;
IF("D" $ x.fileattrib, i, nStartAt) })

// Sort sub-directories only
ASORT(aDIZ, 1, nStartAt, {|x,y| x.filename < y.filename})
nStartAt++

// Sort only the non-directory files.
// Sort Array based on the first subarray
// element
nLEN := LEN(aDIZ)

* IF nLEN > 1
* ASORT( aDIZ, nStartAt,, {|x,y| CompareFile(x.filename,y.filename) } )
* ASORT( aDIZ, nStartAt,, {|x,y| x.filename < y.filename})
* ENDIF

FOR nCount = 1 to nLEN
IF "D" $ aDIZ[nCount,F_ATTR] .and. .not. ;
(aDIZ[nCount,F_NAME] = "." .or. ;
aDIZ[nCount,F_NAME] ="..")
RECUDIRS(p_path+aDIZ[nCount,F_NAME]+"\",p_wildcard,bblock)
ELSE
EVAL(bblock,p_path,aDIZ[nCount,F_NAME])
ENDIF
NEXT

ST_EXIT() // clear Stack
RETURN

FUNCTION DOWWORK(xPath,bDir)
? xPath,bDir
RETURN NIL

*
* eof
*

greetings by OHR
Jimmy


TSDing

unread,
Mar 3, 2004, 2:23:23 PM3/3/04
to

This is done in CA-Visual Objects 2.X. Not sure it will work in CA-Clipper
or not.

aOption := ASort(aOption,,,{|x, y| x[1] <= y[1]})

Regards
TSDing

"Rajeev Chavan" <sschavan...@eth.net> wrote in message
news:c21esr$1ne4b0$1...@ID-214544.news.uni-berlin.de...

Gary Stark

unread,
Mar 3, 2004, 3:39:31 AM3/3/04
to
What a lot of work!

See Ding's reply for a simple, workable solution.


--
g.
Gary Stark
gst...@RedbacksWeb.com
http://www.RedbacksWeb.com

Marcos Nogueira

unread,
Mar 3, 2004, 5:54:25 AM3/3/04
to
TSDing,

> This is done in CA-Visual Objects 2.X. Not sure it will work in CA-Clipper
> or not.
>
> aOption := ASort(aOption,,,{|x, y| x[1] <= y[1]})

Yes it will. And there's no need to assign the expression to aOption - only
ASort(...) is enough.

Best regards,

Marcos Nogueira
S. Paulo - Brazil


Rajeev Chavan

unread,
Mar 3, 2004, 10:35:57 AM3/3/04
to
Hi All
Thanks for your response. I want to achieve as follows.
First I want to sort on 2nd element and then on 1st element. following is a
small example

SET DATE BRIT
aArr := {}
aadd(aArr,{5,ctod("12/2/04")})
aadd(aArr,{2,ctod("15/12/03")})
aadd(aArr,{1,ctod("16/1/04")})
aadd(aArr,{3,ctod("15/12/03")})
aadd(aArr,{6,ctod("15/12/03")})
aadd(aArr,{4,ctod("16/1/04")})
aadd(aArr,{9,ctod("25/12/01")})
aadd(aArr,{7,ctod("15/12/02")})

After sort the array should appear as follows

{9,ctod("25/12/01")}
{7,ctod("15/12/02")}
{2,ctod("15/12/03")}
{3,ctod("15/12/03")}
{6,ctod("15/12/03")}
{1,ctod("16/1/04")}
{4,ctod("16/1/04")}
{5,ctod("12/2/04")}

asort(aArr,,,|x,y| x[2] < y[2])
or
asort(aArr,,,|x,y| x[1] < y[1])
will sort only on one element

AUGE_OHR
Your program I could not understand. It did not run properly. I will have to
look into detail what went wrong

With Best Wishes


Rajeev Chavan
email to : sschavan at eth dot net
>

Rajeev Chavan <sschavan...@eth.net> wrote in message
news:c21esr$1ne4b0$1...@ID-214544.news.uni-berlin.de...

Maurizio la Cecilia

unread,
Mar 3, 2004, 11:37:51 AM3/3/04
to
Hi Rajeev,
try this:
asort(aArr,,,|x,y| DToS(x[2]) + Str(x[1], 3, 0) < DToS(y[2]) + Str(y[1], 3,
0))
Should work. If not so check your libs....
Regards

--
_________________________________
Maurizio la Cecilia
Software consultant and Clipper expert
Foggia (Italy)
-----------------------------------------------------------------
"Rajeev Chavan" <sschavan...@eth.net> ha scritto nel messaggio
news:c24tsk$1pf7dc$1...@ID-214544.news.uni-berlin.de...

TSDing

unread,
Mar 4, 2004, 3:50:37 AM3/4/04
to
oops! thanks

Regards
TSDing

"Marcos Nogueira" <marcos...@arenainformatica.com.br> wrote in message
news:c24dlg$1olisd$1...@ID-76797.news.uni-berlin.de...

Ray Marron

unread,
Mar 3, 2004, 11:48:53 AM3/3/04
to
"Rajeev Chavan" <sschavan...@eth.net> wrote in message
news:c24tsk$1pf7dc$1...@ID-214544.news.uni-berlin.de...
...

> First I want to sort on 2nd element and then on 1st
> element. following is a small example
>
> SET DATE BRIT
> aArr := {}
> aadd(aArr,{5,ctod("12/2/04")})
> aadd(aArr,{2,ctod("15/12/03")})
> aadd(aArr,{1,ctod("16/1/04")})
> aadd(aArr,{3,ctod("15/12/03")})
> aadd(aArr,{6,ctod("15/12/03")})
> aadd(aArr,{4,ctod("16/1/04")})
> aadd(aArr,{9,ctod("25/12/01")})
> aadd(aArr,{7,ctod("15/12/02")})
>
> After sort the array should appear as follows
>
> {9,ctod("25/12/01")}
> {7,ctod("15/12/02")}
> {2,ctod("15/12/03")}
> {3,ctod("15/12/03")}
> {6,ctod("15/12/03")}
> {1,ctod("16/1/04")}
> {4,ctod("16/1/04")}
> {5,ctod("12/2/04")}

A couple of solutions to choose from:

asort(aArr,,, {|x, y| dtos(x[2])+str(x[1], 3, 0) <= dtos(y[2])+str(y[1], 3,
0)})
asort(aArr,,, {|x, y| x[2] < y[2] .or. (x[2] = y[2] .and. x[1] <= y[1])})

The one with all the conversions to string is probably the slower of the
two, but I didn't test either.

Remember: The code block evaluates to true when the two elements are
already in sorted order!

--
Ray Marron

AUGE_OHR

unread,
Mar 3, 2004, 5:08:14 PM3/3/04
to
hi,

> What a lot of work!
> See Ding's reply for a simple, workable solution.

Yes, but having a multidimensional array most times you want to sort more
than one time and it is allways not easy to read which element you a just
working with.

this example with Directory() also show some techincs to "subsort" MDA
and like the Name say it can be used recursiv up to 4096x4096 (W98)
elements to sort ...

greetings by OHR
Jimmy


> AUGE_OHR wrote:
> > Example sorting Directory() multidimensional array :

> > compile with -N
> > #include "Directry.ch"
> > #include "Common.ch"

AUGE_OHR

unread,
Mar 3, 2004, 4:53:33 PM3/3/04
to
hi,

> AUGE_OHR
> Your program I could not understand. It did not run properly. I will have
to
> look into detail what went wrong

exclude the line : #include "dll.ch" (this was Xbase++ for my DowWork() )
and compile with -N switch

greetings by OHR
Jimmy


Maurizio la Cecilia

unread,
Mar 3, 2004, 5:54:50 PM3/3/04
to
Obviously i've missed the brackets in the codeblock.... Sorry

--
_________________________________
Maurizio la Cecilia
Software consultant and Clipper expert
Foggia (Italy)
-----------------------------------------------------------------

"Maurizio la Cecilia" <m.lac...@vizzavi.it> ha scritto nel messaggio
news:zRn1c.45734$Kc3.1...@twister2.libero.it...

Rajeev Chavan

unread,
Mar 4, 2004, 10:19:56 AM3/4/04
to
Hi All
Thanks all especially Maurizio and Ray both of you have come with identical
solutions. Though Ray has given two solutions.I am sure they will workI will
try them and come back if there is any problem.Also AUGE_OHR's solution I
still haven't tried but I will try soon.

With Best Wishes
Rajeev Chavan
Ray Marron <m...@privacy.net> wrote in message
news:c2529p$1lmhal$1...@ID-88259.news.uni-berlin.de...
0 new messages