Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reordering date variables 2

Status
Not open for further replies.

SitesMasstec

Programmer
Sep 26, 2010
523
Brasil
Hello dear colleagues:

I have four Date variables, dA, dB, dC and dD. Each stores a date. For example:
dA=11/16/2018
dB=12/30/2016
dC=08/15/2018
dD=10/20/2018

I want to reorder these dates (first is the newest) in memory, without using file/cursor, so I can transform in:
dA=11/16/2018
dB=10/20/2018
dC=08/15/2018
dD=12/30/2016

Is there a VFP 9 function to do this?



Thank you,
SitesMasstec
 
No, there is not VFP function that will sort items that are stored in different variables. You must first copy them, either to an aray or a cursor, then sort them, then copy them back.

Something like this will do it:

Code:
LOCAL ARRAY Temp(4)
Temp(1) = Da
Temp(2) = Db
Temp(3) = Dc
Temp(4) = Dd

ASORT(Temp)

Da = Temp(1)
Db = temp(2)
Dc = temp(3)
Dd = Temp(4)

It's a bit heavy-handed, but it will do what you want.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
And here's a slightly more generic solution. In this case, you can have as many or as few variables as you like; you are not limited to four. For simplicity, I will assume that you name the varibles D1, D2, D3, ...., rather than Da, Db, Dc. That way, you can have more than 26 variables if you need to.

Code:
lnCount = 4  && Number of date variables
LOCAL ARRAY Temp(lnCount)

FOR lnI = 1 TO lnCount
  lcVar = "D" + TRANSFORM(lnI)
  Temp(lnI) = &lcVar
ENDFOR 

ASORT(Temp)

FOR lnI = 1 TO lnCount
  lcVar = "D" + TRANSFORM(lnI)
  &lcVar = Temp(lnI)
ENDFOR

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 

I got the result I want, thanks Mike for your advice:

Code:
DECLARE dultco(4)
* 
dultco(1)=YCDULT
dultco(2)=YCDULT2
dultco(3)=YCDULT3
dultco(4)=YCDULT4

= ASORT(dultco,1,4,1)   && Sort the array, START=1, N.OF ELEMENTS=4, ORDER=1=DESCENDING

YCDULT = dultco(1)
YCDULT2= dultco(2)
YCDULT3= dultco(3)
YCDULT4= dultco(4)

As I have to reorder only 4 variables, it is a clear solution whenever I look at the code.

But as soon as I have the same problem with more than 10 variables, I will adopt your last solution presented here.


Thank you,
SitesMasstec
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top