Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Multiple sorting on 2-dimensional arrays
Message
From
18/02/1999 07:34:16
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00188684
Message ID:
00188792
Views:
27
>Hello, All!
>
>If I have a 2-dimensional array like so:
>
>
>2 A
>2 B
>1 A
>1 B
>

>
>..and I want it sorted to look like:
>
>
>1 A
>1 B
>2 A
>2 B
>

>
>Anybody know a trick for this? I tried ASORT(2), and then ASORT(1), and vice versa, but wrong results.
>Yes, I know you can do this programatically.
>
>Thanks in advance!
>Mark
Hi Mark,
Unfortunately all ways I can think of are programmatic (a ready dll, fll ?). Anyway 2 cents here if you're interested :
*!* Shorter code, lower performance
* Would count for a trick maybe :)
* But any cursor, whatever the size is, would write to disk
* so larger the array, slower the result I think
* unless not already a cursor or table
*!*	dimension myarray[15,2]
*!*	=rand(-1)
*!*	for ix = 1 to 15
*!*		myarray[ix,1] = int(rand()*5)
*!*		myarray[ix,2] = chr(int(rand()*5)+65)
*!*	endfor
*!*	clear
*!*	create cursor mycursor (c1 i, c2 c(1))
*!*	append from array myarray
*!*	select * from mycursor into array myarray order by 1,2
*!*	use in "mycursor"
*!*	for ix = 1 to 15
*!*		? myarray[ix,1], myarray[ix,2]
*!*	endfor


*!* Larger code, faster performance - MAXELEMCOUNT dropped to MAXELEMCOUNT*2/3
clear
#define ROWCOUNT 15
#define COLCOUNT 2
#define SORTEXPRESSION "str(myarray[ix,1])+ myarray[ix,2]"

dimension myarray[ROWCOUNT,COLCOUNT]
=rand(-1)
? "Before sort..."
for ix = 1 to ROWCOUNT
	myarray[ix,1] = int(rand()*5)
	myarray[ix,COLCOUNT] = chr(int(rand()*5)+65)
	? myarray[ix,1], myarray[ix,2]
endfor

**************
=InsDelColumn(@myarray,ROWCOUNT,COLCOUNT)  && Add a temp sort column
for ix = 1 to ROWCOUNT
	myarray[ix,(COLCOUNT + 1)] = eval(SORTEXPRESSION)
endfor
=asort(myarray,(COLCOUNT + 1))  && Do sort
=InsDelColumn(@myarray,ROWCOUNT,COLCOUNT, .T.) && Remove temp sort column
**************

? "After sort..."
for ix = 1 to ROWCOUNT
	? myarray[ix,1], myarray[ix,2]
endfor


* function InsDelColumn
* Insert or remove a column from array at the end column
* Resize array to tnRows, tnCols if remove, tnRows, tnCols+1 if add
function InsDelColumn
lparameters taTempArray, tnRows, tnCols, tlRemove
local ix

* Conv. to one dim - just for ease of coding array subscript
dimension taTempArray[tnRows * (tnCols + 1)] 

if tlRemove && Is it a column removal ?
	for ix = ROWCOUNT to 1 step -1
		=adel(taTempArray,(ix-1) * (tnCols + 1) + (tnCols + 1))
	endfor
else       && or column add ?
	for ix = 1 to tnRows
		=ains(taTempArray,(ix-1) * (tnCols + 1) + (tnCols + 1))
	endfor
endif

&& Back to two dim. with resize
dimension taTempArray[tnRows,( tnCols + iif(tlRemove,0,1) )]
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform