View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > StructSort PreviousNext

StructSort

Returns a sorted array of the top level keys in a structure. Sorts using alphabetic or numeric sorting, and can sort based on the values of any structure element.

An array of top-level key names (strings), sorted by the value of the specified subelement.

Structure functions

StructSort(base, sortType, sortOrder, pathToSubElement)

Structure functions

Parameter

Description

base

A ColdFusion struct with one field (an associative array).

sortType

  • numeric
  • text: case sensitive (all lower-case letters precede the first upper-case letter). Default.
  • textnocase

sortOrder

  • asc: ascending (a to z) sort order. Default.
  • desc: descending (z to a) sort order

pathToSubElement

String or a variable that contains one.

Path to apply to each top-level key, to reach element value by which to sort. Default: nothing (top-level entries sorted by their own values).

The pathToSubElement string does not support array notation, and only supports substructures of structures.

This function does not sort or change the structure.

<cfscript>
   salaries = StructNew() ;
   employees = StructNew() ;
   departments = StructNew() ;
   for ( i=1; i lt 6; i=i+1 )
   {
      salary = 120000 - i*10000 ;
      salaries["employee#i#"] = salary ;
      
      employee = StructNew() ;
      employee["salary"] = salary ; 
      // employee.salary = salary ;
      employees["employee#i#"] = employee ;
      
      departments["department#i#"] = StructNew() ;
      departments["department#i#"].boss = employee ;      
   }
</cfscript>

<cfoutput>
<p>list of employees based on the salary (text search): <br>
1) #ArrayToList( StructSort( salaries ) )#<br>
2) #ArrayToList( StructSort( salaries, "text", "ASC" ) )#<br>
3) #ArrayToList( StructSort( salaries, "textnocase", "ASC" ) )#<br>
4) #ArrayToList( StructSort( salaries, "text", "DESC" ) )#<br>
<p>list of employees based on the salary (numeric search): <br>
5) #ArrayToList( StructSort( salaries, "numeric", "ASC" ) )#<br>
6) #ArrayToList( StructSort( salaries, "numeric", "DESC" ) )#<br>
<p>list of employees based on the salary (subfield search): <br>
7) #ArrayToList( StructSort( employees, "numeric", "DESC", "salary" ) )#<br>
8) #ArrayToList( StructSort( employees, "text", "ASC",  "salary" ) )#<br>
<p>list of departments based on the salary (sub-sub-field search): <br>
9) #ArrayToList( StructSort( departments, "text", "ASC", "boss.salary" ) )#<br>
</cfoutput>

<!--- add an invalid element and test that it throws an error --->
<p><p>
<cfset employees[ "employee4" ] = StructNew()>
<cftry>
   <cfset temp = StructSort( employees, "text", "ASC", "salary" )>
   <cfoutput>We have a problem - this was supposed to throw an exception!<br>
</cfoutput> <cfcatch type="any"> <cfoutput> ERROR: <b>This error was expected!</b><br> #cfcatch.message# - #cfcatch.detail#<br> </cfoutput> </cfcatch> </cftry>

Contents > CFML Reference > ColdFusion Functions > StructSort PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


sanaullah said on Aug 8, 2003 at 8:25 AM :
The most powerfull feature of structures is, For example you have one structure which would hold other structure values as well. and we can split the structure up to single numeric or text value for sorting the structure .
jce1975 said on Feb 3, 2005 at 11:47 AM :
How would you sort the values in a Structure in alphabetical order based upon the Key? Is it possible? Something like this?

<cfset Values = structNew()>
....................
<!--- code to enter values into the structure--->
....................
<cfset temp = StructSort( values, "text", "ASC", Key )>

Thanks for your help ahead of time!
OneTruth said on Apr 28, 2005 at 11:06 AM :
to get a list sorted based on the structure key values you can do the following:

<cfset temp = ListSort(StructKeyList(myStruct), "text")>
Martyn Hills said on May 10, 2005 at 3:17 AM :
I have a 2d structure to contain user information but how do i order the entire list by an element in the structure??

Here is what im storing with examples

name : John Smith
service : development
department : beer development

How would i sort my structure by service, department then by name??

Any help would be great as its driving me mad!!!!!!
Martyn Hills said on May 10, 2005 at 3:41 AM :
Its ok as i sorted it using the following

<cfset directoryQuery = QueryNew("FirstName,LastName,Service,Department,ext")>

<cfoutput query="rs_searchResults">

<cfset service = getToken(distinguishedName,3,",")>
<cfset service = trim(getToken(service,2,"="))>

<cfif not findnocase(service,bannedlist)>
<cfscript>
newRow = QueryAddRow(directoryQuery);
QuerySetCell(directoryQuery, "FirstName","#rs_searchResults.givenname#" );
QuerySetCell(directoryQuery, "LastName", "#rs_searchResults.sn#" );
QuerySetCell(directoryQuery, "Service","#service#" );
QuerySetCell(directoryQuery, "department","#rs_searchResults.department#" );
QuerySetCell(directoryQuery, "ext","#replacenocase(rs_searchResults.telephonenumber,' ','','all')#" );
</cfscript>
</cfif>
</cfoutput>

<cfquery dbtype="query" name="sorted">
SELECT *
FROM directoryQuery
ORDER BY service, department, lastname, firstname
</cfquery>

As you can see i was trying to sort active directory. CFLDAP couldnt do it for me. Hope this helps others as it certainly helped me
mowebdev said on Aug 2, 2005 at 12:13 PM :
Helped me! Thanks.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funca104.htm