Solved

Sorting directory listing problem in Coldfusion

Posted on 2004-10-14
10
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to setup ssl access to internal local machine 4 124
JVM encoding. How to change encoding. 27 121
DNS, website, godaddy 6 94
Company website 6 46
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to Import and export files in WordPress. 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 : Click on Too…

749 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