Solved

Sorting directory listing problem in Coldfusion

Posted on 2004-10-14
10
163 Views
Last Modified: 2013-12-24
Hi,

I'm using the <cfdirectory> tag to list some directories in my server in ascending order.  The problem I'm having is that the output is not correct.  For example,  assume I have a bunch of directories called A,10,2,B.  The output of the <cfdirectory> tag, would list the directories as 10,2,A,B.  As you can see,  the directory named 10 should be located after directory 2.

Any ideas on how I displayed them correctly?

Thanks,

Mark
0
Comment
Question by:mlam01
10 Comments
 
LVL 21

Expert Comment

by:pinaldave
ID: 12315333
try something like this ....
<cfdirectory
   directory="#GetDirectoryFromPath(GetTemplatePath())#"
   name="myDirectory"
   sort="name ASC, size DESC">

Regards,
---Pinal
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 12315953
That is actually the correct order.

Don't forget the name is a string not an integer, if it was an integer the sort order would be wrong.

You could probably do a Query of Query and only sort on the first character.
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12320680
As Tacobell mentioned it is sorting correctly.

Just not the way you want.

To get the way you want you need to know what is the max length that any could be.

Then pad any name that is shorter than than with 0.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:mlam01
ID: 12321001
Can anyone post an example? Thanks, Mark
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12321295
An example of what?  padding?  if so then you need to know more about your desired sorting.

For example does A come before or after 2?

What is the maximum length of a string that you will be sorting?

Should the sort match the order on your server?  If so then no changes are needed as if you look on the server sort orders will look like :

1
10
100
11
12
13
14
2
20
3
30
4
400
etc...
0
 

Author Comment

by:mlam01
ID: 12324307
mrichmon,

Thank you for replying.

To answer your questions, ideally, A would come after 2.  I guess I could limit the length of the directory's name string to three.  And when I look at the sort order on my server through MS Explorer,  it is listed in the order I want,  that's it, 1, 2,10,A,B,...

Mark
0
 

Author Comment

by:mlam01
ID: 12324317
Hi everyone,

Someone suggested that I could do something clever with CFQUERY (dbtype="query") and re query my recordset. Not sure how to do it though...
0
 

Author Comment

by:mlam01
ID: 12342282
OK.  I figured it out by myself.  I hope my solution helps others who have the same problem.
So, basically I created two arrays.  One to store numeric names and the other to store string names.  Then,  I would append the two arrays together into one list.

<cfdirectory action="list" directory="path_to_directory" name="myDirList" sort="name ASC">

<cfset dirsArray=arraynew(1)>
<cfset dirsAlphaArray=arraynew(1)>
<cfset i=1>
<cfset j=1>
<cfloop query="myDirList">
      <cfif isNumeric(myDirList.name)>
            <cfset dirsArray[i]=myDirList.name>
            <cfset i = i + 1>
      <cfelse>
            <cfset dirsAlphaArray[j]=LCase(myDirList.name)>
            <cfset j = j + 1>
      </cfif>
</cfloop>

<cfset listAlphaDir="#ArrayToList(dirsAlphaArray,",")#">
<cfset listDir="#ArrayToList(dirsArray,",")#">
<cfset sortedDir="#ListAppend(ListSort(listDir,"Numeric"),ListSort(listAlphaDir,"Text"),",")#">
<cfoutput>#sortedDir#</cfoutput>
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12380997
Question answered by asker or dialog valuable.
Closed, 275 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now