Skip to main content
Known Participant
September 27, 2013
Question

Date array and Sorting

  • September 27, 2013
  • 1 reply
  • 1882 views

I have a date array that i created from a database.  I am trying to sort the dates into the correct order with the upcoming on top.... the best would be that after the date has passed it would go to the bottom of the list. Can anyone tell me if I am on the right path with what I have so far and give me any hints?  thanks (the second loop give me an error)

<!--- Declare query array --->

<cfset dateArray = arraynew(1)>

<!--Poppulate Array row by row--->

<cfloop query="CDE_Dates2">

          <cfset dateArray[currentRow][1] = event_date>

</cfloop>

<!--- Sort Array Dates --->

<cfloop index="i" from="1" to="#arrayLen(dateArray)#">

          <cfset dateVar = DateFormat(dateArray,"YYYY/MM/DD")>

</cfloop>

    This topic has been closed for replies.

    1 reply

    Carl Von Stetten
    Legend
    September 27, 2013

    I think I read that as you want dates sored in an order something like this:  future dates at the top, sorted in ascending order (most imminent first to farthest future last), then past dates at the bottom in ascending order (oldest first to most recent past last).

    The easiest way would be to either do this when you query the database (in your CDE_Dates2) or do a query-of-query on CDE_Dates2.  You can do it using a union and adding an extra column for sorting.  Here's some SQL pseudo-code:

    SELECT dateColumn, otherColumn, anotherColumn, 0 AS sortOverride

    FROM someTable

    WHERE dateColumn > <cfqueryparam value="#Now()#" cfsqltype="cf_sql_date">

    UNION

    SELECT dateColumn, otherColumn, anotherColumn, 1 AS sortOverride

    FROM someTable

    WHERE dateColumn <= <cfqueryparam value="#Now()#" cfsqltype="cf_sql_date">

    ORDER BY sortOverride, dateColumn

    This will force the future dates to the top and put past dates at the bottom.

    You could do the same thing in a QofQ.

    -Carl V.

    Known Participant
    September 27, 2013

    I understand how that code works ... I am confused about the sortOverride?  is this a table column or just a temp name?

    Carl Von Stetten
    Legend
    September 27, 2013

    It's a temp column being added by the query - it won't exist in the table, just when the query is run.

    -Carl V.