E9.2 Sort Dates Earliest to Latest

FrankCLT

Well Known Member
Hello All,

I have 4 dates that I need to sort...earliest to oldest....these dates are being returned through a BSFN.....I can use a NER, BSFN, or event rules. Can someone point me in the right direction? Thanks in advance....

FrankCLT

Example:
From:
BF 1 = 07/15/2022
BF 2 = 03/22/2021
BF 3 = 05/21/2022
BF 4 = 01/22/2021

To:
01/22/2021
03/22/2021
05/21/2022
07/15/2022
 
There are multiple sort algorithms. Is the number of dates always constant? You can do a simple bubble sort of 4 dates.

Craig
 
Yes, dates will be consistent. They will be 3 or 4 dates, that hasn’t been decided. I have looked at insertion and bubble sorts, but I’m getting issues comparing the dates within the array. Is there something similar to a Math or String Compare for Dates in JDE C?
 
It might be overkill, but how about writing the dates to a work table keyed by the dates?
 
That about that….our CNC and DBA’a might not like that approach.
 
Yes, dates will be consistent. They will be 3 or 4 dates, that hasn’t been decided. I have looked at insertion and bubble sorts, but I’m getting issues comparing the dates within the array. Is there something similar to a Math or String Compare for Dates in JDE C?
C API

Code:
long DateDifference (JDEDATE FAR *lpjdDateStart, JDEDATE FAR *lpjdDateEnd);

returns:
0 = the same
Negative = end date is earlier than start date
Positive = end date is later than start date
 
I think I may have a working solution with 4 dates….

I will post the code and ask for input as to what I may do better or more efficiently.
 
If you ultimately need the list of dates in ER code you will need to store the array of dates in memory (allocated by the C BSFN) and then create some kind of iterator function to be called from ER code... this is because ER doesn't have any type of array construct.

So, in C BSFN you will have three basic choices for this. A straight up dynamically allocated array, a Linked List (LINKEDLIST) or jdeCache. If you opt for the last one, provided your data set doesn't contain duplicates, you can simply make the JDEDATE the primary key in the jdeCache struct def and it will sort it for you. If you do have dups you can still use jdeCache and make the date the PK, but just have another field as a counter and then your iterator function will simply return the same date record == to the count.
 
I was able to use an Insertion Sort and it seems to be working fine….
 
Cool you have it working
I'd have firstly written to the government to say can we please get rid of this silly m/d/y format we use please as no one else does :)
then I'd have created a workfile, split them into D M Y and created an index over D M Y and sorted like that
 
for (i = 1; i <= n - 1; i++)
{
j = i;
while (j > 0 && (SingleValueComp((LPVOID)&aSortedETADates[j - 1].jdDSDates, (LPVOID)&aSortedETADates[j].jdDSDates, EVDT_JDEDATE) > 0))
{
JDEDATECopy(&jdKeyDate, &aSortedETADates[j].jdDSDates);
JDEDATECopy(&aSortedETADates[j].jdDSDates, &aSortedETADates[j-1].jdDSDates);
JDEDATECopy(&aSortedETADates[j - 1].jdDSDates, &jdKeyDate);

jdeStrncpy(szKeyWarehouse, aSortedETADates[j].szDSWarehouse, DIM(szKeyWarehouse));
jdeStrncpy(aSortedETADates[j].szDSWarehouse, aSortedETADates[j - 1].szDSWarehouse, DIM(aSortedETADates[j - 1].szDSWarehouse));
jdeStrncpy(aSortedETADates[j - 1].szDSWarehouse, szKeyWarehouse, DIM(aSortedETADates[j - 1].szDSWarehouse));
j--;
}
}

This is how I got mine to work.
 
Back
Top