Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sorting multi-dimensional arrays
Message
De
19/03/2002 07:20:21
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
18/03/2002 20:00:28
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00634318
Message ID:
00634400
Vues:
21
>Hi,
>
>I am having a hard time understanding the help file for asort() in vfp 7. I have a multidimentional array and I want to sort it by column1+column2. Can anybody point me in the right direction?
>
>Thanks,
>Chris

Chris,
asort() operates on one column only. You need to sort one column at a time (keep in mind sorting an array needs all array elemnts in the same column are of same type) :
* ROWCOUNT * COLCOUNT <= 65000 (Max array elements)
#Define ROWCOUNT 13000
Dimension myarray[ROWCOUNT,5]
=rand(-1)
For ix = 1 to ROWCOUNT
  myarray[ix,1] = int(rand()*5)
  myarray[ix,2] = int(rand()*5)+date()
  myarray[ix,3] = int(rand()*5)
  myarray[ix,4] = int(rand()*5)
  myarray[ix,5] = chr(int(rand()*5)+65)
Endfor

Asort(myarray)         && VFP builtin - Sort on 1st col
axsort(@myarray,1,5,1) && Sort on 5th Desc - keep previous sort on 1st
axsort(@myarray,5,2)   && Sort on 2nd - keep previous sort on 5th

******************
*!*	function axSort
*!*	Extended array sort
*!*	taSortArray - Array to sort passed by ref
*!*	tnKeepColumn - Column number that's already sorted on
*!*	tnSortColumn - Column to sort
*!*	tnDirection - Omitted or 0 ascending, else descending
******************
Function axsort
  Lparameters taSortArray, tnKeepColumn, tnSortColumn, tnDirection
  tnDirection = iif(empty(tnDirection),0,1)
  lnRows = alen(taSortArray,1)
  lnRows2Sort=0
  lnSortStart = tnSortColumn
  luElem = taSortArray[1,tnKeepcolumn]
  For ix=1 to lnRows
    If taSortArray[ix,tnKeepcolumn] # luElem
      Asort(taSortArray,lnSortStart, lnRows2Sort, tnDirection)
      lnSortStart = (ix-1)*alen(taSortArray,2)+tnSortColumn
      lnRows2Sort = 1
      luElem = taSortArray[ix,tnKeepcolumn]
    Else
      lnRows2Sort = lnRows2Sort+1
    Endif
  Endfor
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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform