-
Notifications
You must be signed in to change notification settings - Fork 0
Comparator
Felipe Ribeiro edited this page Feb 19, 2014
·
2 revisions
The Comparator object is used in many data structures and algorithms that involve comparison and sorting of elements.
It can be created from a comparator function that has to attend to this requirements:
- The function receives two parameters (a and b) that should be compared.
- If the elements are equal, the function should return 0
- If the first parameter (a) is lower than the second(b) return a number < 0
- If the first parameter (a) is greater than the second return a number > 0
If no comparator function is passed to the Comparator object, it will use a default implementation that reproduces the behavior of the native comparison operators: >, < and ==.
function defaultComparator(a, b) {
if (a == b) return 0;
return a < b ? -1 : 1;
}