Skip to main content
January 20, 2011
Answered

ListSort sorting dynamic lists?

  • January 20, 2011
  • 1 reply
  • 3349 views

Hi I have been able of course to sort a standard list with the ListSort function like so:

<cfset myList=ListSort("wango, jango, tango, cash","TextNoCase","ASC")>

<cfoutput>#myList#</cfoutput>

which results in just a text with no html:

cash, jango, tango, wango

But, what if I need to sort a dynamic list thats being displayed from another page with a cfinclude?

<cfset myList=ListSort("some_lists","TextNoCase","ASC")>

I've tried wrapping the cfinclude in a cfsavecontent variable="some_lists"> and then output that variable with no luck. Can this not be done? or am I going about this all wrong? Thanks for any help in advance!

This topic has been closed for replies.
Correct answer ilssac

Ok I think I have it!! My code:

<cfset results = deserializeJSON(url.data)>
  <cfloop collection = '#results#' item = 'itemName' >
    <cfset item = results[itemName]>
    <cfset vendorList = listSort(structKeyList(item.vendors),"textnocase","ASC")>
    <!---NEW LINE--->
    <cfloop list= '#vendorList#' index = 'vendorName'>
      <h2>#vendorName#</h2>
      <ul class="myList">
        <cfset productArray = item.vendors[vendorName]>
        <cfset productArray = arraySort(productArray)>
        <!--- NEW LINE--->
        <cfloop array='#productArray#' index = 'productName'>
          <li style="margin-left:30px;">#productName#</li>
        </cfloop>
      </ul>
    </cfloop>
  </cfloop>


Then I commented out the line: <cfset productArray = arraySort(productArray)>.

Now it seems to sort the Vendors correct and throughout the complete application! Maybe there is way way to sort an array of structs, if this was the issue at all anyway, but obvioulsy there was something it didnt like.


teedoffnewbie wrote:


Now it seems to sort the Vendors correct and throughout the complete application!

Yes, the vendors are sorted by these lines creating a sorted list of the vendor keys and then looping for that list, rather the the verdor structure itself.

<cfset vendorList = listSort(structKeyList(item.vendors),"textnocase","ASC")>
  
    <cfloop list= '#vendorList#' index = 'vendorName'>

teedoffnewbie wrote:

but obvioulsy there was something it didnt like.

Yes, it did not like the line you commented out,  because I did not use the array sort function correctly.

        <cfset productArray = arraySort(productArray)>

Should have been:

<cfset testBoolean = arraySort(productArray,"textnocase")>

That I posted in my earlier reply.  This line would also sort the products under each vendor.

1 reply

ilssac
Inspiring
January 20, 2011

teedoffnewbie wrote:

<cfset myList=ListSort("some_lists","TextNoCase","ASC")>

Is that the actual line of code you used which would try and sort a list that contains the single literal string "some_lists".

Or did you use an actual variable in the function which would look like.

<cfset myList = ListSort(some_lists,"textNoCase","ASC")>


Or one of the less clean versions using pound|hash|# symbols

<cfset myList = ListSort(#some_lists#,"textNoCase","ASC")>

<cfset myList = ListSort("#some_lists#","textNoCase","ASC")>

January 20, 2011

ahh sorry, yes some_lists would have been a variable. I actually thought I was close just now. I changed the ListSort function to:

<cfsavecontent variable = "myList">
<cfinclude template="vendorProductList.cfm">
</cfsavecontent>
<cfset myList = ListSort("#myList#","TextNoCase","ASC","li")>
<cfoutput>#myList#</cfoutput>

I thought this worked at first, but it doesnt because my lists are somewhat unique. They are generated by jQuery and called back and displayed with an ajax function.

When I use the "li" as a delimiter, I get the lists ordered alphabetically I think, but it also seems to throw a bunch of junk in with it. Characters like < and misplaced letters like the following:

    l>     

ale-type:none;">          

jango

tango

wango

meproductusyou

Plus that doesn work period since each dynamcially created ul will have list items of its own, then the next ul will have its own list items. So really that would simply order the FIRST li items in order, then order the "sub" list items underneath as you can see above with "meproductusyou".

But if I leave out the li as a delimiter, I get no sorting whatsoever.

January 20, 2011

I should try to explain in better detail I guess...lol

These lists are user defined. The user enters data in input fields. Then these values are passed to my action.cfm page and looped through in a two dimensional array...so a loop nested in another loop.

The results of the loops are then saved with cfsavecontent and sent to a temp file....vendorProductList.cfm and this file is sent back to my page as a cfinclude.

Now, again the user has the option of creating as many ul's as she needs via jQuery, there's no way to determine in advance how many ul's nor how many list items are going to be sorted.