|
Home > Archive > Programming with dBASE > October 2005 > Array sorting
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I use dir() to get an array of files from a directory. Is there a way to sort this array by time and date descending? Have tried various things but can only get list sorted by date.
Below is the test code I was using.
Thanks,
Rick
#define ARRAY_DIR_NAME 1 // Manifest constants for columns
#define ARRAY_DIR_SIZE 2
#define ARRAY_DIR_DATE 3
#define ARRAY_DIR_TIME 4
#define ARRAY_DIR_ATTR 5
aOBJ = new Array
nOBJ = aOBJ.dir("C:\TESTOBJ\*.*o")
* list out unsorted first
for nOBJfile = 1 to nOBJ
cFileToCopy=UPPER(RT
RIM(LTRIM(aOBJ[nOBJfile, ARRAY_DIR_NAME ])))
cDate= dtoc(aOBJ[nOBJfile, ARRAY_DIR_DATE ])
cTime= aOBJ& #91;nOBJfile,ARRAY_D
IR_TIME ]
? cFileToCopy,cDate,cT
ime
next
WAIT
* sort it
nRows = alen( aOBJ, 1 ) // Determine # of rows
* need sort by date/time descending
aOBJ. sort(ARRAY_DIR_DATE,
nRows,1 )
for nOBJfile = 1 to nOBJ
cFileToCopy=UPPER(RT
RIM(LTRIM(aOBJ[nOBJfile, ARRAY_DIR_NAME ])))
cDate= dtoc(aOBJ[nOBJfile, ARRAY_DIR_DATE ])
cTime= aOBJ& #91;nOBJfile,ARRAY_D
IR_TIME ]
? cFileToCopy,cDate,cT
ime
next
WAIT
| |
| Todd Kreuter 2005-10-07, 3:26 am |
| Rick wrote:
>
> I use dir() to get an array of files from a directory. Is there a way to sort this array by time
and date descending? Have tried various things but can only get list
sorted by date.
Hi Rick,
Try the following which combines the date and time in an added column.
Todd Kreuter [dBVIPS]
*** Copy and paste into .prg (watch word wrap) ***
aDir = new ArrayDir()
aDir.dir("C:\Temp\*.*")
aDir.sortDateTime(1)
aDir.list()
class ArrayDIR of Array
#define ARRAY_DIR_NAME 1
#define ARRAY_DIR_SIZE 2
#define ARRAY_DIR_DATE 3
#define ARRAY_DIR_TIME 4
#define ARRAY_DIR_ATTR 5
function SortDateTime(nOrder)
********************
*
local nRow, nCol, i
if PCOUNT() = 0
nOrder = 0
endif
nRow = aLen(this, 1)
nCol = aLen(this, 2)
if nCol # 6
this.grow(2)
endif
for i = 1 to nRow
this[i,6] = DTOS(this[i,3]) + this[i,4]
endfor
this.sort(6, nRow, nOrder)
return
function List
*************
local i, cName
for i = 1 to aLen(this, 1)
cName = SPACE(40).stuff(0, this[i,1].length,
this[i,1].left(40))
? cName, this[i, 2], this[i,3], this[i, 4]
endfor
return
endclass
| |
|
| Hi Todd,
Many thanks...works great.
Rick
Todd Kreuter Wrote:
> Rick wrote:
> and date descending? Have tried various things but can only get list
> sorted by date.
>
> Hi Rick,
>
> Try the following which combines the date and time in an added column.
>
> Todd Kreuter [dBVIPS]
>
> *** Copy and paste into .prg (watch word wrap) ***
>
> aDir = new ArrayDir()
> aDir.dir("C:\Temp\*.*")
> aDir.sortDateTime(1)
> aDir.list()
>
>
> class ArrayDIR of Array
>
> #define ARRAY_DIR_NAME 1
> #define ARRAY_DIR_SIZE 2
> #define ARRAY_DIR_DATE 3
> #define ARRAY_DIR_TIME 4
> #define ARRAY_DIR_ATTR 5
>
> function SortDateTime(nOrder)
> ********************
*
> local nRow, nCol, i
>
> if PCOUNT() = 0
> nOrder = 0
> endif
>
> nRow = aLen(this, 1)
> nCol = aLen(this, 2)
>
> if nCol # 6
> this.grow(2)
> endif
>
> for i = 1 to nRow
> this[i,6] = DTOS(this[i,3]) + this[i,4]
> endfor
>
> this.sort(6, nRow, nOrder)
>
> return
>
> function List
> *************
> local i, cName
>
> for i = 1 to aLen(this, 1)
>
> cName = SPACE(40).stuff(0, this[i,1].length,
> this[i,1].left(40))
>
> ? cName, this[i, 2], this[i,3], this[i, 4]
>
> endfor
>
> return
>
>
> endclass
|
|
|
|
|