keysort/2
[ISO]keysort(
+Pairs,
-Sorted)
Sorts the elements of the list Pairs into ascending standard order (see ref-lte-cte-sot) with respect to the key of the pair structure.
The list Pairs must consist of terms of the form Key-Value. Multiple occurrences of pairs with the same key are not removed.
(The time taken to do this is at worst order (N log N) where N is the length of the list.)
Note that the elements of Pairs are sorted only according to the value of Key, not according to the value of Value.
keysort/2
is stable in the sense that the relative position of
elements with the same key is maintained.
Sorted is type checked since release 4.3 for alignment with the ISO Prolog standard. Previous releases simply failed instead of reporting an error for malformed Sorted.
instantiation_error
type_error
type_error
| ?- keysort([3-a,1-b,2-c,1-a,1-b], X). X = [1-b,1-a,1-b,2-c,3-a]
|?- keysort([2-1, 1-2], [1-2, 2-1]). yes