Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
A second order sort with ASORT()
Message
De
03/07/2005 18:37:37
 
 
À
03/07/2005 10:54:07
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01028620
Message ID:
01028690
Vues:
19
>A second order sort with ASORT()
>--------------------------------------------------
>
>In the following command line, ADIR populates the
>
>"aFiles" array with all of the JPG files in the given folder.
>
>
>nFiles = ADIR(aFiles,"C:\2005_Digital_Pictures\*.jpg")
>
>
>The next line sorts the array by File Date.
>
>= ASORT(aFiles,3)
>
>
>However, it would be nice to have a second order sort
>by File Time (sorted first by File Date and then by File Time).
>
>I've got some ideas about how to do this, but was curious to see
>if someone has already done this.
>
>Thanks!
>Terry :)

ASORT, and this implementation cannot compete with
an ORDER BY of a SELECT, but it has a great merit:
IT DOESN'T WRITE THE DATA IN A TEMPORARY FILE
besides, for not very long array it can be competitive.
Other important thing, can order strings with more than 255 characters.
Attention, has done it and tried for few minutes.
SET TEXTMERGE TO SORTRESULT.TXT NOSHOW ON
\ rows = <<ADIR(aFiles,"C:\windows\*.*")>>
\

\ *************************
\ ***  sort by name asc ***
\ *************************

OutArray()

\ ***************************************************
\ *** sort by date ASC,time ASC,size ASC,name ASC ***
\ ***************************************************

=ASortColumns(@m.aFiles,3,4,2,1)

OutArray()

\ *******************************************************
\ *** sort by date DESC,time DESC,size DESC,name DESC ***
\ *******************************************************
=ASortColumns(@m.aFiles,-3,-4,-2,-1)

OutArray()

SET TEXTMERGE TO
MODIFY FILE "SORTRESULT.TXT" NOWAIT
*ERASE "SORTRESULT.TXT"

PROCEDURE OutArray
	FOR k=1 TO ALEN(aFiles,1)
	  \ <<PADR(aFiles[k,1],40)>> <<aFiles[k,3]>> <<aFiles[k,4]>> <<PADL(aFiles[k,2],20)>>			
	NEXT
	
* PUBLIC
PROCEDURE ASortColumns
PARAMETERS aArray AS @,iCol1,iCol2,iCol3,iCol4,iCol5,iCol6,iCol7,iCol8,iCol9 && ... iCol25

IF ASORT(aArray,ABS(m.iCol1),-1,1-SIGN(m.iCol1))=1 AND PARAMETERS()>2
	PRIVATE aCols,iCol
	DIMENSION aCols[PARAMETERS()-1,2]
	aCols = 0 && Ascending
	FOR iCol=1 TO ALEN(aCols,1)
		STORE EVALUATE("m.iCol"+STR(m.iCol,1)) TO aCols[m.iCol,1]
		IF aCols[m.iCol,1]<0
			STORE 1					TO aCols[m.iCol,2]	&& descending
			STORE -aCols[m.iCol,1]	TO aCols[m.iCol,1]
		ENDIF
	NEXT
	RELEASE iCol
	RETURN ASortSubColumns(1,ALEN(aArray,1),1)
ENDIF

* PROTECTED
PROCEDURE ASortSubColumns(iRowStart,iRowEnd,iLevel)
	PRIVATE iRowGroup
	FOR iRowStart=1 TO m.iRowEnd-1 STEP 0
		FOR iRowGroup=m.iRowStart+1 TO m.iRowEnd
			* VFP SUPPORT NULLS
			IF IIF(ISNULL(aArray[m.iRowGroup,aCols[m.iLevel,1]]),ISNULL(aArray[m.iRowStart,aCols[m.iLevel,1]]);
				,aArray[m.iRowGroup,aCols[m.iLevel,1]]==aArray[m.iRowStart,aCols[m.iLevel,1]])
					LOOP
			ENDIF
			EXIT
		NEXT
		=	m.iRowGroup>m.iRowStart+1	;
		AND ASORT(aArray,AELEMENT(aArray,m.iRowStart,aCols[m.iLevel+1,1]),m.iRowGroup-m.iRowStart,aCols[m.iLevel+1,2])=1;
		AND m.iRowGroup>m.iRowStart+2	;
		AND m.iLevel<ALEN(aCols,1)-1	;
		AND ASortSubColumns(m.iRowStart,m.iRowEnd-1,m.iLevel+1)

		STORE m.iRowGroup TO iRowStart
	NEXT
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform