Key-Valuepairs, terms whose principal functor is (-)/2. List is sorted on Key according to the standard order of terms (see section 4.6.1). Duplicates are not removed. Sorting is stable with regard to the order of the Values, i.e., the order of multiple elements that have the same Key is not changed.
predicate is often used together with library
library(pairs). It can be used to sort lists on different
or multiple criteria. For example, the following predicates sorts a list
of atoms according to their length, maintaining the initial order for
atoms that have the same length.
:- use_module(library(pairs)). sort_atoms_by_length(Atoms, ByLength) :- map_list_to_pairs(atom_length, Atoms, Pairs), keysort(Pairs, Sorted), pairs_values(Sorted, ByLength).