Parameter
        
            
                Last updated on 
                
                    Mar 4, 2024
                
            
            
        
        
    
Description
This function sorts a query where the sorting algorithm is passed at runtime in the form of closure.
Returns
Returns the sorted query.
Category
History
- Adobe ColdFusion (2018 release): Returns the sorted query.
 - Adobe ColdFusion (2016 release): Added the function
 
See also
Syntax
boolean QuerySort(Object query , UDFMethod sortFunc)
Parameters
| 
                    
     | 
            
                
                
                    
     Description  | 
            
        
| 
                    
     query  | 
            
                
                
                    
     (Required) The query to be sorted.  | 
            
        
| 
                    
     sortFunc  | 
            
                
                
                    
     (Required) Sort function to be used.  | 
            
        
Heading 1
<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
Example 2
<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.
