Link to home
Start Free TrialLog in
Avatar of PhotoMan2000
PhotoMan2000

asked on

Sort directory listing with coldfusion

I have the following code and need to sort the resulting list in Alpha order.  I'm sorting a folder and using the file contents to dynamicaly create href links to those file.

I believe I have everything correct but it is sorting like this:
file1, file10, file2, file21, file3

I want it to be: file1, file2, file21, file3

What is missing or incorrect with the code?

Thanks.
<!--- Sets directory path --->
<cfset documentsDir = ExpandPath( "./meetingDocs/TechEnvMeeting11-20-08/" ) />
<cfdirectory directory="#documentsDir#" name="dirQuery" listinfo="all" action="list" type="file">
<!--- Get an array of directory names. --->
<cfset dirsArray=arraynew(1)>
<cfset i=1>
<cfloop query="dirQuery">
<cfif dirQuery.type IS "dir">
    <cfset dirsArray[i]=dirQuery.name>
    <cfset i = i + 1>
</cfif>
</cfloop>
<cfquery dbtype="query" name="finalSort">
select * from dirQuery order by dirQuery.name asc
</cfquery>
<ul>
<cfoutput query="finalSort">
        <li><a href="./meetingDocs/TechEnvMeeting11-20-08/#name#">#name#</a></li>
	</cfoutput>
   </ul>

Open in new window

Avatar of erikTsomik
erikTsomik
Flag of United States of America image

try this
select name from dirQuery order by name
Avatar of PhotoMan2000
PhotoMan2000

ASKER

No change in the order. Also tried:
select dirQuery.name from dirQuery order by dirQuery.name

Avatar of SidFishes
this is expected order by behaviour

order by looks at chars like this

file
   1, 10,
   2, 21,
   3

I'll see if i can knock together something that works for you
Thanks. BTW this is running on a Windows server.  The strange thing is that when I view the file in the folder, it sorts correctly - Windows must sort in a differently?  I know that 1,11,2,21,3 is the correct sort order but humans are dumb!
this is an issue with QoQ's (and is a lot more complicated than I first thought!)

OK. is there any sort of solution or do I have to suffer with it? :)
ASKER CERTIFIED SOLUTION
Avatar of SidFishes
SidFishes
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You did not specify if files in your folder have extension (it is returned as part of #name# variable from <cfdirectory>. My code accounts for file extension if there are any.

Solution below uses combination of regex with NumberFormat() function. Advantage of using NumberFormat() - no matter how many digit filename has in it, all will be listed in proper order.
<cfscript>
 // simulate cfdirectory
 colvalues = listToArray("file10,file124.txt,file2,file23.xls,file3.txt");
 dirQuery = querynew("");
 queryaddcolumn(dirQuery, "name", colvalues);
</cfscript>
 
<cfquery dbtype = "query" name="cfdir_query">
select dirQuery.name 
from dirQuery 
</cfquery> 
 
<cfset  finalQuery = queryNew("fileName,fileOrder")>
 
<cfloop query="cfdir_query">
  <cfset fileName_order = NumberFormat(REReplace(name, "^\D+(\d+)(\..*)?$", "\1"))>
  <cfset temp = QueryAddRow(finalQuery)>
  <cfset temp = QuerySetCell(finalQuery, "fileName", name)>
  <cfset temp = QuerySetCell(finalQuery, "fileOrder", fileName_order)>
</cfloop>
 
<cfquery dbtype = "query" name="finalSort">
	select fileName, fileOrder
	from finalQuery 
	order by fileOrder
</cfquery> 
<cfdump var="#finalSort#">

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you sorted it out?
Hi I sort of don't need this any more and am not sure what to do about the points.

Admin - please advise.  No need to refund them to my account.
since both eszaq and i provided working answers you should probably split the points rather than delete