Sorts a query.
querySort(query, sortFunction)
→ returns boolean
query.sort(sortFunction)
querySort(query, column_name [, direction])
syntax. As of 5.1.0.13 it accepts a callback function. When called as a member function query.sort() is chainable.
//build a query
exampleQuery = queryNew(
"id,createdDate",
"integer,timestamp",
[
{id=1, createdDate=dateAdd("d", randRange(-100, 100), now())},
{id=2, createdDate=dateAdd("d", randRange(-100, 100), now())},
{id=3, createdDate=dateAdd("d", randRange(-100, 100), now())},
{id=4, createdDate=dateAdd("d", randRange(-100, 100), now())},
{id=5, createdDate=dateAdd("d", randRange(-100, 100), now())}
]
);
//sort it
querySort(exampleQuery, function(rowA, rowB) {
return dateCompare(rowA.createdDate, rowB.createdDate);
});
//dump it
writeDump(exampleQuery);
//build a query
exampleQuery = queryNew(
"id,createdDate",
"integer,timestamp",
[
{id=1, createdDate=dateAdd("d", randRange(-100, 100), now())},
{id=2, createdDate=dateAdd("d", randRange(-100, 100), now())},
{id=3, createdDate=dateAdd("d", randRange(-100, 100), now())},
{id=4, createdDate=dateAdd("d", randRange(-100, 100), now())},
{id=5, createdDate=dateAdd("d", randRange(-100, 100), now())}
]
);
//sort it
exampleQuery.sort(function(rowA, rowB) {
return -(dateCompare(rowA.createdDate, rowB.createdDate));
});
//dump it
writeDump(exampleQuery);
//build a query
exampleQuery = queryNew(
"id,description",
"integer,varchar",
[
{id=1, description="dog"},
{id=2, description="cat"},
{id=3, description="bird"},
{id=4, description="zebra"},
{id=5, description="snake"}
]
);
//sort it
exampleQuery.sort(function(rowA, rowB) {
return compare(rowA.description, rowB.description);
});
//dump it
writeDump(exampleQuery);
//build a query
exampleQuery = queryNew(
"id,description",
"integer,varchar",
[
{id=1, description="dog"},
{id=2, description="cat"},
{id=3, description="bird"},
{id=4, description="zebra"},
{id=5, description="snake"}
]
);
//sort it
querySort(exampleQuery, function(rowA, rowB) {
return -(compare(rowA.description, rowB.description));
});
//dump it
writeDump(exampleQuery);
//build a query
exampleQuery = queryNew(
"id,description",
"integer,varchar",
[
{id=3, description="dog"},
{id=5, description="cat"},
{id=4, description="bird"},
{id=1, description="zebra"},
{id=2, description="snake"}
]
);
//sort it
querySort(exampleQuery, function(rowA, rowB) {
return val(rowA.id) < val(rowB.id) ? -1 : val(rowA.id) == val(rowB.id) ? 0 : 1;
});
//dump it
writeDump(exampleQuery);
Signup for cfbreak
to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.