# E9.2Sort Dates Earliest to Latest

#### FrankCLT

##### Active 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

Anyone? Help!!!

#### craig_welton

##### Legendary Poster
There are multiple sort algorithms. Is the number of dates always constant? You can do a simple bubble sort of 4 dates.

Craig

#### FrankCLT

##### Active Member
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?

#### Neal Scarr

##### Active Member
It might be overkill, but how about writing the dates to a work table keyed by the dates?

#### FrankCLT

##### Active Member
That about that….our CNC and DBA’a might not like that approach.

#### craig_welton

##### Legendary Poster
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

#### FrankCLT

##### Active Member
That is for 2 dates?

#### FrankCLT

##### Active Member
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.

#### BOster

##### Legendary Poster
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.

#### FrankCLT

##### Active Member
I was able to use an Insertion Sort and it seems to be working fine….

#### johnd

##### Well Known Member
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

#### FrankCLT

##### Active Member
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))
{

j--;
}
}

This is how I got mine to work.