sort()
Sort the elements of the API instance's result set.
Description
The sort()
method provides a way of sorted the data in an API instance's result set, which can be particularly useful if you then want to use that data for displaying to the end user - for example as a select
list for a search input. This method should not be confused with order()
which is used to order for records in the DataTable.
This method makes use of the fact that DataTables API objects are "array like", in that they inherit a lot of the abilities and methods of the Javascript Array
type.
In this case, this method is a proxy for the Javascript Array.prototype.sort
method and is provided as a utility method for the DataTables API. For more information about the original method, please refer to the Mozilla MDN documentation for sort
.
Type
function sort( [ fn ] )
- Description:
Sort the elements of the API instance's result set.
- Parameters:
Name Type Optional 1 fn
Yes - default: This is a standard Javascript sort comparison function. It accepts two parameters:
- Value 1 to compare
- Value 2 to compare
And expects a return which indicated the sorted position of the two values. Less that 0 indicates that the first value comes before the second, greater than 0 indicates that the first value comes after the second and a return of 0 indicates that they are identical.
- Returns:
The original API instance with the result set sorted as defined by the sorting conditions used.
Example
Sort the data from a column:
var table = new DataTable('#myTable');
var data = table
.column(0)
.data()
.sort();