sort/2 — sorts a list of terms
keysort/2 — sorts a list of Key-Data pairs
sort(List, SortedList) keysort(List, SortedList)
sort/2 sorts the
List according to the standard order. Identical elements, as defined by
==/2, are merged, so that each element appears only once in
List to be a list of terms of the form:
Key-Data. Each pair is sorted by the
Key alone. Pairs with duplicate
Keys will not be removed from
A merge sort is used internally by these predicates at a cost of at most N(log N) where N is the number of elements in
The following examples illustrate the use of
?- sort([orange,apple,orange,tangelo,grape],X). X = [apple,grape,orange,tangelo] yes. ?- keysort([warren-davidh, bowen-kenneth, warren-davids, bowen-david, burger-warren], X). X = [bowen-kenneth,bowen-david,burger-warren, warren-davidh,warren-davids] yes.
The following example shows the way structures with the same principal functor are sorted:
?- sort([and(a,b,c), and(a,b,a,b), and(a,a), and(b)],Sorted). Sorted = [and(a,a),and(a,b,a,b),and(a,b,c),and(b)] yes.