Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting directory listing problem in Coldfusion

Posted on 2004-10-14
10
Medium Priority
?
169 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
Suggested Courses

886 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