liveBook · Manning

Comparison function

Also called Comparator

A comparison function or strategy compares two values and indicates their relative ordering, and sometimes whether they are equivalent.

How 4 Manning books explain it

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.

Read how Modern C explains comparison function →

Also covered in

What to know first

Related concepts

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.