Create a comparator function which can be passed into Array.prototype.sort. It accepts either a property name or a mapping function. Optionally, you can pass a custom compare function (e.g. for localeCompare use cases).
The first argument of castComparator is called the mapping. This can be either:
Function: If mapping is a function, it maps the input values to a comparable value.
Property Name: If mapping is a property name, it maps the input values to a property of the input values with a comparable value.
Function: If mapping is a function, it maps the input
values to a comparable value.
Property Name: If mapping is a property name, it maps the
input values to a property of the input values with a comparable
value.
Optionally, you can pass a custom compare function that receives
the mapped values and returns a number. If not provided, values are
compared with the < and > built-in operators. A positive number
means the “right value” is greater than the “left value”, a
negative number means the “left value” is greater than the “right
value”, and 0 means both values are equal.
Sorts an array in place.
This method mutates the array and returns a reference to the same array.
@param ―
compareFn Function used to determine the order of the elements. It is expected to return
a negative value if the first argument is less than the second argument, zero if they're equal, and a positive
value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
Function: If mapping is a function, it maps the input
values to a comparable value.
Property Name: If mapping is a property name, it maps the
input values to a property of the input values with a comparable
value.
Optionally, you can pass a custom compare function that receives
the mapped values and returns a number. If not provided, values are
compared with the < and > built-in operators. A positive number
means the “right value” is greater than the “left value”, a
negative number means the “left value” is greater than the “right
value”, and 0 means both values are equal.
Sorts an array in place.
This method mutates the array and returns a reference to the same array.
@param ―
compareFn Function used to determine the order of the elements. It is expected to return
a negative value if the first argument is less than the second argument, zero if they're equal, and a positive
value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
[11,2,22,1].sort((a, b) => a - b)
sort(
constcompareByFullName:_.Comparator<{
id:number;
firstName:string;
lastName:string;
}>
compareByFullName)
// [Alice, Charlie, Drew]
Compare Function
Optionally, you can pass a custom compare function that receives the mapped values and returns a number. If not provided, values are compared with the < and > built-in operators.
A positive number means the “right value” is greater than the “left value”, a negative number means the “left value” is greater than the “right value”, and 0 means both values are equal.
Function: If mapping is a function, it maps the input
values to a comparable value.
Property Name: If mapping is a property name, it maps the
input values to a property of the input values with a comparable
value.
Optionally, you can pass a custom compare function that receives
the mapped values and returns a number. If not provided, values are
compared with the < and > built-in operators. A positive number
means the “right value” is greater than the “left value”, a
negative number means the “left value” is greater than the “right
value”, and 0 means both values are equal.
Sorts an array in place.
This method mutates the array and returns a reference to the same array.
@param ―
compareFn Function used to determine the order of the elements. It is expected to return
a negative value if the first argument is less than the second argument, zero if they're equal, and a positive
value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.