Solved

Sorting directory listing problem in Coldfusion

Posted on 2004-10-14
10
161 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
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 manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
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. …

705 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

14 Experts available now in Live!

Get 1:1 Help Now