query.sort()

Sorts the query based on the column specified and the order criteria given. Modifies the original query object

query.sort( columnNameOrSortFunc=any, direction=string )

Returns: Query

Argument Description
columnNameOrSortFunc
any, required
  • a list of names
  • a single column name
  • or a function used as comparator with the following constructor

function(struct row1, struct row2){ return true / false;}.

Alias: column_name, columnNames, columnName, name, names, column_names, sort, function, udf, sortFunction, sortFunc

direction
string, optional

a list of directions or a single direction definition (asc, desc),

the list must have the same length as the columnName list.

Only used when the second argument defines a list of column names.

Alias: directions, dir

Examples

people = QueryNew( "name,dob,age", "varchar,date,int", [
    [ "Susi", CreateDate( 1970, 1, 1 ), 70 ],
    [ "Urs" , CreateDate( 1995, 1, 1 ), 40 ],
    [ "Fred", CreateDate( 1960, 1, 1 ), 50 ],
    [ "Jim" , CreateDate( 1988, 1, 1 ), 30 ]
]);

Dump( var=people, label="people - original query" );

people.sort('name', 'asc'); dump(var=people, label='people - sorted by name');

people2 = people.sort(function(row1, row2){ return compare(arguments.row1.age, arguments.row2.age); }); dump(var=people2, label='people - sorted by age');

See also