liveBook · Manning

Sorting algorithm

As explained in Clojure: The Essential Reference

The arrangement of elements into an order determined by a comparison criterion.

Use sort to order a collection with compare by default, or supply a comparator. Use sort-by to compare items through a single-argument key function, such as an :age field. section 8.3.5

Check the input and result:

  • Inputs must be seq-compatible; items must be mutually comparable. Incompatible items can cause ClassCastException, and unsupported inputs such as transients or scalars can cause IllegalArgumentException. section 8.3.5
  • The result is an ordered sequence; nil or empty input produces an empty list. Sorting a native array mutates that input. section 8.3.5

How Clojure: The Essential Reference covers it

How deep it goes
Defines input, comparator, output, and notable exceptions, but does not detail the sorting algorithm's internals.
Examples
Demonstrates sorting keywords, numbers, maps by age, and mixed values by string conversion.
What it assumes
Assumes collection elements can be compared, while specifying the accepted input and comparator contracts.
What you can do after
Apply sort or sort-by with a comparator or key function to produce an ordered sequence.
How it connects
Connects sorting to comparators, compare, and sort-by key functions.

Also explained in

Compare how 18 books explain sorting algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.