Using QuerySort

I am trying to use ColdFusion 2016 query sorting

I base sorting on Array by Raymond Camden sorting

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 mistake or am I doing it wrong? Or the sort member function does not matchQuerySort()

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

+4
source share
1 answer

An example of Ray was for CF10 beta. See comment . In the CF10 + release, the comparator should return 1, 0, or -1 .

Use this. When executed, a.name > b.nameit simply 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

+6
source

Source: https://habr.com/ru/post/1629609/


All Articles