• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Date array and Sorting

Community Beginner ,
Sep 27, 2013 Sep 27, 2013

Copy link to clipboard

Copied

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>

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 27, 2013 Sep 27, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 27, 2013 Sep 27, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 27, 2013 Sep 27, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 27, 2013 Sep 27, 2013

Copy link to clipboard

Copied

What if the year spans 12 and 13 .... can I still make this work?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 27, 2013 Sep 27, 2013

Copy link to clipboard

Copied

Can you elaborate a bit?  I'm not sure what you mean.

-Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 27, 2013 Sep 27, 2013

Copy link to clipboard

Copied

So say my dates are between now and a year ago... so I want the dates to appear as such:

September 29,2013

October 26, 2013

December 14, 2013

December 14, 2013

October 20, 2012

December 1, 2012

January 26, 2013

March 2, 2013

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 27, 2013 Sep 27, 2013

Copy link to clipboard

Copied

Yeah, that pseudo code should work fine as is.

-Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2013 Sep 30, 2013

Copy link to clipboard

Copied

Ok I have thise query....

<CFQUERY name="dates" datasource="xxx">

SELECT dateColumn, otherColumn, anotherColumn, 0 AS sortOverride

FROM someTable

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

</CFQUERY>

I only get

10/26/2013

12/14/2013

12/14/2013

12/14/2013

There are like 17 dates the go back a year.   How do I get those to show up as well.? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 01, 2013 Oct 01, 2013

Copy link to clipboard

Copied

@TheScarecrow,

You're missing half the query logic from the example I gave you.  You need the UNION and the partial query that follows.  The part you used grabs the future dates; the part you left out grabs the past dates.

-Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 02, 2013 Oct 02, 2013

Copy link to clipboard

Copied

LATEST

Thanks that did it.  I got it working. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation