Show TOC

Sorting and Filtering in XML ModelsLocate this document in the navigation structure

If you use an XML model for data binding, sorting and filtering is implemented in JavaScript because all data is available on the client. You can use custom methods for sorting and filtering in an XML model. To define custom methods, set the fnCompare method on the Sorter object or the fnTest method on the Filter object after creating it.

The fnTest method of the Filter gets the value to test as the only parameter and returns, whether the row with the given value should be filtered or not. To implement a filter, use the following code :
var oFilter = new sap.ui.model.Filter("property");
oFilter.fnFilter = function(value) {
    return (value > 100);
};
The fnCompare method of the Sorter gets the two values to compare as parameters and returns -1, 0 or 1, dependent which of the two values should be ordered before the other. To implement a sorter, use the following code:
var oSorter = new sap.ui.model.Sorter("property");
oSorter.fnCompare = function(value1, value2) {
    if (value1 < value2) return -1;
    if (value1 == value2) return 0;
    if (value1 > value2) return 1;
};