11.3.107 keysort/2

Synopsis

keysort(+List1, -List2)

Sorts the elements of the list List1 into ascending standard order (see ref-lte-cte-sot) with respect to the key of the pair structure.

Arguments

List1
list of pair, must be a proper list of proper pairs
List2
list of pair

Description

The list List1 must consist of terms of the form Key-Value. Multiple occurrences of any term 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 List1 are sorted only according to the value of Key, not according to the value of Value.

keysort is stable in the sense that the relative position of elements with the same key is maintained.

Exceptions

instantiation_error
If List1 is not properly instantiated
type_error
If List1 is not a list of key-value pair.

Examples

     | ?- 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

See Also

ref-lte-cte-sor.


Send feedback on this subject.