Comparison function
A comparison function or strategy compares two values and indicates their relative ordering, and sometimes whether they are equivalent.
Clojure: The Essential Reference
The Java Comparator interface defines an object contract for comparing two values through a compare method.
- How it is used
- Orders values for sorting and for sorted maps and sets, with custom comparisons when default ordering is unsuitable.
- How deep it goes
- Defines the three-way result, works through custom ordering and tie-breakers, and identifies incompatible inputs and NaN behavior.
- Examples
- Uses reversed string ordering, equal-length vector collisions, and vector tie-breakers to demonstrate comparator behavior.
- What you can do after
- Apply it via sorted-map-by and sorted-set-by to choose an order without unintentionally collapsing distinct keys or elements.
Read how Clojure: The Essential Reference explains comparison function →
Secrets of the JavaScript Ninja, Third Edition
A compare function is a sorting callback that returns a negative, positive, or zero number to order two items.
- How it is used
- Supplies application-specific ordering to JavaScript’s built-in array sort method.
- How deep it goes
- Defines the three return-value cases and works through numeric and string sorting, leaving the sorting algorithm to the JavaScript engine.
- Examples
- Uses subtraction to order numbers and `Intl.Collator.compare` to order names.
- What you can do after
- Apply it via `Array.prototype.sort` to specify numeric or locale-aware name ordering without implementing a sorting algorithm.
Read how Secrets of the JavaScript Ninja, Third Edition explains comparison function →
Modern C, Third Edition
A comparison function is a caller-supplied function that determines the ordering or equality of two data elements for an algorithm such as sorting or searching.
- How it is used
- Deploys comparison functions as callbacks that give qsort and bsearch an ordering for otherwise type-generic data.
- How deep it goes
- Explains the callback contract, void-pointer conversions, overflow hazard, and sortedness requirement without developing ordering theory further.
- Examples
- Uses unsigned comparison code, an invalid subtractive integer comparator, and qsort or bsearch interfaces.
- What you can do after
- Apply qsort and bsearch with comparison callbacks that preserve element types, ordering results, overflow safety, and sortedness.
Read how Modern C, Third Edition explains comparison function →
Modern C
A comparison function is a callback that determines the ordering of two values by returning a negative, zero, or positive result.
- How it is used
- Deploys callbacks to give qsort and bsearch type-independent ordering information for array elements.
- How deep it goes
- Explains the return contract, void-pointer conversions, and integer-overflow pitfall, but defers a complete search or sorting implementation.
- Examples
- Uses unsigned comparison code, an overflowing integer subtraction comparator, and qsort/bsearch declarations.
- What you can do after
- Apply it via qsort and bsearch by writing a consistent comparator and supplying the correct element size.
Also covered in
- 100 Java Mistakes and How to Avoid Them
- Java Persistence with Spring Data and Hibernate epub
- Kotlin in Action, 2E
- Data-Oriented Programming in JavaIn early access (MEAP)
- JavaScript in DepthIn early access (MEAP)
What to know first
- arrayAn array is an ordered collection of elements organized in one or more dimensions and accessible by position or index.
- sorting algorithmA sorting algorithm is a procedure that rearranges elements of a collection into an order determined by an ordering criterion.
- minimum valueA minimum value is the least value in a set or collection according to the applicable comparison rules.
Related concepts
- bsearchused by · bsearch is a C standard-library function that performs a binary search on a sorted array using a caller-provided comparison function.
- qsortused by · qsort is a C standard-library function that sorts an array using a caller-supplied comparison function.
- sorting algorithmused by · A sorting algorithm is a procedure that rearranges elements of a collection into an order determined by an ordering criterion.
- Array.prototype.sortused by · A JavaScript method that sorts an array in place using a default ordering or a supplied comparison function.
- callbackkind of · A callback is a function or other callable supplied or registered with a component for invocation at a specified event, condition, or point in execution.
Questions about comparison function
What is comparison function?
A comparison function or strategy compares two values and indicates their relative ordering, and sometimes whether they are equivalent.
Which Manning books explain comparison function?
Clojure: The Essential Reference, Secrets of the JavaScript Ninja, Third Edition, Modern C, Third Edition and Modern C.
What should I know before learning comparison function?
Manning books that teach comparison function usually introduce array, sorting algorithm and minimum value first.
What is comparison function a kind of?
Comparison function is a kind of callback. A callback is a function or other callable supplied or registered with a component for invocation at a specified event, condition, or point in execution.
What is comparison function also called?
Comparison function is also called Comparator.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



