Description

This function sorts a query where the sorting algorithm is passed at runtime in the form of closure.

Returns

True if the sort is successful.

Category

Query functions

History

New in Adobe ColdFusion (2016 release)

See also

QueryEach, QueryFilter, QueryKeyExists

Syntax

boolean QuerySort(Object query , UDFMethod sortFunc)

Parameters

Parameter Description
query (Required) The query to be sorted.
sortFunc (Required) Sort function to be used.

Example

<cfscript>
               qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"};
               sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions);

               sortStatus = QuerySort(sampleQuery, function(e1, e2){
                              return compare(e1.TITLE, e2.TITLE);
               });
               writeOutput("Sort Successful: " & sortStatus);
               writeDump(sampleQuery);
</cfscript>

The script sorts the query by Title. The script returns an array of structs.

Using member function

<cfscript>
       myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
       status=myResult.sort(function (c1,c2){
             return compare(c1.DEPARTMENT,c2.DEPARTMENT);
       });
       WriteDump(myResult);
</cfscript>

The output is a table with values in the column DEPARTMENT sorted.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy