ColdFusion CFDIRECTORY fun with sorting
Last week on our CFUG's technical email list, a question was asked by @cosmic: I have created a dynamically generated XML based off of the contents of a directory using CFDIRECTORY. The problem I am having is that when it outputs the XML it doesn't put the files in the correct order that I want. It sorts them as 1.jpg, 10.jpg, 11.jpg,12.jpg, 13.jpg, 14.jpg, 15.jpg ,16.jpg, 17.jpg,18.jpg, 19.jpg, 2.jpg, 20.jpg and so on. I want them to sort as 1.jpg ,2.jpg, 3.jpg, 4.jpg, 5.jpg, 6.jpg. The only fix I have run into is to add zeros on to the beginning of the files 01.jpg, 02.jpg. Is there a way around this so I don't have to add zero to my 1,000 files?
An answer was quickly given by @lockjw: Use QueryAddColumn to add a sorting column the the CFDIRECTORY result. Parse the file name to get the numeric value - put that into the new column. Use query of query to output results, sorting on the newly added column.
However, @cosmic was still stuck and asked for some code. So I did what @lockjw explained, but with a directory of 14 text files named 1.txt thru 14.txt. It worked great...
<cfdump var="#dirlist#">
<cfset queryaddcolumn(dirlist,"mysort",ArrayNew(1))>
<cfloop query="dirlist">
<cfset num = ListGetAt(name,1,".")>
<cfset querysetcell(dirlist,"mysort",num,currentrow)>
</cfloop>
<cfdump var="#dirlist#">
<cfquery name="dirlist_sorted" dbtype="query">
select *
from dirlist
order by mysort
</cfquery>
<cfdump var="#dirlist_sorted#">

http://www.forta.com/blog/index.cfm/2006/11/1/impr...
<cfdirectory action="LIST" directory="#expandpath('.')#" LISTINFO="name" name="mydirlist" sort="Name">