Parameter
This function sorts a query where the sorting algorithm is passed at runtime in the form of closure.
True if the sort is successful.
Category
History
New in Adobe ColdFusion (2016 release)
See also
boolean QuerySort(Object query , UDFMethod sortFunc)
|
Parameter |
Description |
|
query |
(Required) The query to be sorted. |
|
sortFunc |
(Required) Sort function to be used. |
<cfscript>
myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",
[
{id=1,name="One",amount=15},
{id=2,name="Two",amount=18},
{id=3,name="Three",amount=32},
{id=4,name="Four",amount=53}
]);
sortedQuery=QuerySort(myQuery,function(obj1,obj2){
return compare(obj1.name,obj2.name) // compare on names
})
writeOutput("The sorted query is:")
writeDump(sortedQuery)
</cfscript>
Output
<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.
<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.
Sign in to your account