Search code examples
coldfusioncoldfusion-9cfml

In Coldfusion how do you order a list alphabetically then numerically


Say I have the following list:

<cfset myList = "1a,2b,3c,aa,bb,cc" >

How do I sort this list so that it will be "aa,bb,cc,1a,2b,3c"? In other words, I want anything that starts with a number to be at the end of the list and in order of the number it starts with.


Solution

  • @Henry had a working answer for my CF9 server however when I plugged in my list with 2000 or so items in it, this method would have a significant delay, so I ended up coming up with my own way of doing this which turned out to be super fast:

    <cfset digits = "0,1,2,3,4,5,6,7,8,9">
    <cfset numList = "">
    
    <cfloop index="item" list="#arguments.input#">
        <cfif ListFindNoCase(digits, Left(item, 1)) >
            <cfset numindex = ListFindNoCase(arguments.input, item) >
            <cfset numList = ListAppend(numList, item)>
            <cfset arguments.input = ListDeleteAt(arguments.input, numindex) >
        </cfif>
    </cfloop>
    
    <cfset numList = ListSort(numList, "textnocase", "asc") >
    <cfset arguments.input = ListSort(arguments.input, "textnocase", "asc") >
    <cfset arguments.input = ListAppend(arguments.input, numList)>
    
    <cfreturn arguments.input >