CollectionSorter
A class which encapsulates a single sorter operation which may be applied to a Collection to order its elements in a specific way.
A CollectionSorter generally has two properties:
property- The name of a property in collection objects by which to sortdirection- The sort direction,'ASC'or'DESC'.
It may also be configured with just a sortFn function which returns the desired comparison result when passed two objects to compare. Note that this does not require or use the property config. Two collection items are passed for comparison.
Further configurations may affect how the sorter is applied:
convert- A function which, when passed the property value from a collection object, returns the value to sort by.
A CollectionSorter may be configured to encapsulate a sortFn by passing that function as the sole parameter to the constructor:
new CollectionSorter((lhs, rhs) => {
lhs = lhs.customerDetails.companyName.toLowerCase();
rhs = rhs.customerDetails.companyName.toLowerCase();
if (lhs < rhs) {
return -1;
}
else if (lhs > rhs) {
return 1;
}
else {
return 0;
}
});
Configs
7
Configs
7Configure as false to have string comparisons case-insensitive.
When using property, this may be specified as a function which takes the raw property value and returns the value to actually sort by.
| Parameter | Type | Description |
|---|---|---|
value | * |
The id of this Sorter for when used by a Collection Collection.
By default the id is the property value.
The name of a property of collection objects which yields the value to sort by.
A function which takes the place of using property and direction. The function is passed two objects from the collection to compare and must return the comparison result.
| Parameter | Type | Description |
|---|---|---|
first | * | The first value to compare |
second | * | The second value to compare |
direction | ASC | DESC | The sort direction |
Returns 1 if first value is greater than second value, -1 if the opposite is true
or 0 if they're equal
Use localeCompare() when sorting, which lets the browser sort in a locale specific order. Set to true,
a locale string or a locale config to enable.
Enabling this has big negative impact on sorting performance. For more info on localeCompare(), see
MDN.
collection.addSorter({ property : 'name', useLocaleSort : 'sv-SE' });