Solved

Sorting directory listing problem in Coldfusion

Posted on 2004-10-14
10
162 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
 

Author Comment

by:mlam01
ID: 12321001
Can anyone post an example? Thanks, Mark
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IPA - can it be run on a web server? 3 142
Apache SSL and mod_rewrite not working 8 122
Add server behaviors to Dreamweaver CC 2015 2 129
Question to locate the problem 18 79
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

911 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

21 Experts available now in Live!

Get 1:1 Help Now