Skip to main content
amy_yl
Participant
October 28, 2011
Question

How to use an array to organize and sort?

  • October 28, 2011
  • 1 reply
  • 816 views

This is a basic ColdFusion question, but I don't do this enough to have progressed very far. Ugh.

I have a folder of documents on my web site that are newsletters, all in the filename format of mmddyy.cfm. I have created an display of these as links noting the date in text by using this:

<cfdirectory

          action="list"

          directory="D:\inetpub\leithpetwerks\BunFun\BFpages"

          name="Stuff"

          sort="Name">

<cfoutput query="Stuff">

<cfif (RIGHT(Stuff.name,4) EQ ".cfm") or (RIGHT(Stuff.name,5) EQ ".html")>

<a href="#Stuff.name#">#MonthAsString(left(Stuff.name,2))# #right(left(Stuff.name,4),2)#, #right(left(Stuff.name,6),2)#</a><BR>

</cfif>

</cfoutput>

But what I need to do is have them presented by chronological order... not the order they are present in the file system. Conceptually I imagine I'd throw then into an array and somehow address the sorting... but I have no idea how. Can someone help me?

Thanks in advance.

    This topic has been closed for replies.

    1 reply

    Inspiring
    October 28, 2011

    There are a couple of core ColdFusion objects/concepts that will greatly help you out here.

    First - the variable coming back from your <cfdirectoy> call is a CF Query object.  Coldfusion provides you with a way of re-ordering and re-organizing data stored in an existing query object using <cfquery> and the Query-of-a-Query functionality (e.g. dbtype="query").  Details are on Adobe Docs for <cfquery>

    Second, and this is outside of your original question but I noticed it in your code, CF has really great list parsing functions.  Particularly the ability to indicate an alternate list delimter.  Where am I going with this?  If you treat your file name as a delimeted list where the "." is the list delimeter, you'll notice that your file names are actually 2 element lists: e.g. MyFileName.cfm = Item 1: My FileName, Item 2: cfm

    Using the ListLast(Stuff.name, ".") will extract out the file extension from your filenames much more maintanably (is that a word?) than

    <cfif (RIGHT(Stuff.name,4) EQ ".cfm") or (RIGHT(Stuff.name,5) EQ ".html")>

    Especially if you wind up with other extensions in that directory.  Try it out and see how it works.

    Inspiring
    October 28, 2011

    When you say chronological order, do you mean the file name date or the o/s last modified date? If you mean the o/s modified date, try changing the cfdirectory "sort" to order by dateLastModified instead of name. I do not recall offhand if it is a string or date. But give it a shot.

    BKBK
    Community Expert
    Community Expert
    October 29, 2011

    <cfdirectory

              action="list"

              directory="D:\inetpub\leithpetwerks\BunFun\BFpages"

              name="Stuff"

              sort="dateLastModified Asc">

    or

    <cfdirectory

              action="list"

              directory="D:\inetpub\leithpetwerks\BunFun\BFpages"

              name="Stuff"

              sort="dateLastModified Desc">