querySort

Sorts a query.

querySort(query, sortFunction) → returns boolean

Member Function Syntax

query.sort(sortFunction)

querySort Argument Reference

query query
Required

The query to sort

sortFunction function
Required

CF 2016+ or Lucee 5.1.0.13+ a function with two arguments each corresponding to a query row. The function returns -1 if first row is less than second, 0 if equal, or 1 if first row is greater than second. Lucee 4.5+ accepts a column name to sort by instead of a function.

direction string

Lucee 4.5+ Only supported on Lucee when second argument is a column name instead of a callback function.

Compatibility

Lucee:

Lucee also supports 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.

Links more information about querySort

Examples sample code invoking the querySort function

//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);

Fork me on GitHub