当我必须按术语Order by子句获取结果时如何使用Queryfilter函数

时间:2018-11-28 20:08:32

标签: coldfusion coldfusion-2016 qoq

尝试执行以下代码时出现错误:

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=27},
                    {id=5,name="Five",amount=43},
                    {id=6,name="Six",amount=71}
            ]);

get=myQuery.filter(function(obj){
   return (order by obj.name )
});
writeOutput("The filtered query is:")
writeDump(get);

错误显示“在第22行的第16行发现无效的CFML构造”

我知道上面的错误是因为以obj.name作为参数返回。但是我将如何做而不会出现任何错误?谢谢。

1 个答案:

答案 0 :(得分:3)

要执行order by,您需要执行sort(),而不是filter()

<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=27},
                    {id=5,name="Five",amount=43},
                    {id=6,name="Six",amount=71}
            ]);

myQuery.sort("name", "desc");
writeOutput("The filtered query is:")
writeDump(myQuery);
</cfscript>

https://trycf.com/gist/f827f61a03a173a3b13ad01a2cd13dce/lucee5?theme=monokai

已编辑:无需引用get,只需引用查询本身即可。