Search code examples
coldfusionclosurescoldfusion-2016

Using QuerySort


I am trying to use ColdFusion 2016 Query sort

I am basing the sort on Array sort by Raymond Camden

http://www.raymondcamden.com/2012/08/14/Another-ColdFusion-10-Closures-Post/

<cfscript>
    qryTest = QueryNew("ID,Name");
    qryTest.AddRow([ 
        {id=1,name="One"}, 
        {id=2,name="Two"}, 
        {id=3,name="Three"}, 
        {id=4,name="Four"} 
    ]);
    qryTest.sort(function(a, b) {
       return a.name > b.name;
    });
    writedump(qryTest);
</cfscript>

enter image description here

Is this a bug or am I doing it wrong? Or is sort member function not the same as QuerySort()

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-m-r/querysort.html#main-pars_header


Solution

  • Ray's example was for the CF10 beta. See this comment. In the CF10+ release, the comparator must return 1, 0, or -1.

    Use this. When doing a.name > b.name it just returns true/false. You need to return 1/-1.

    <cfscript>
        qryTest = QueryNew("ID,Name");
        qryTest.AddRow([ 
            {id=1,name="One"}, 
            {id=2,name="Two"}, 
            {id=3,name="Three"}, 
            {id=4,name="Four"} 
        ]);
        qryTest.sort(function(a, b) {
           return a.name > b.name ? 1 : -1;
        });
        writedump(qryTest);
    </cfscript>
    

    enter image description here