Builtin predicates - Sorting
Predicates in this group are used to efficiently sort lists into ascending or descending order. The sorting algorithm used is merge sort, which provides a stable sort where elements with the same key keep their relative ordering in the result.
keysort/2, keysortd/2, msort/2, msortd/2, rkeysort/2, rkeysortd/2, sort/2, sortd/2
  keysort/2 - sorts a keyed list into ascending order.
- Usage
 - keysort(List, Sorted)
 
- Description
 - Unifies Sorted with a sorted version of List. The elements should be functors of arity 2. The first argument to the functor is used as a key to be ordered according to (@<)/2. The tail of List can be a variable; the sorted version will have nil as its tail. 
 
- If an element is not a functor of arity 2, then the element itself will be used as the key.
 
- Errors
 
instantiation_error - List is a variable.  
type_error(list, List) - List is not a list or the tail of List is not nil or a variable. 
- Examples
  keysort([a - 1, y - 2, b - 8, m - 6], [a - 1, b - 8, m - 6, y - 2])
- Compatibility
 - SWI-Prolog has a keysort/2 predicate that performs the same function as this predicate with some minor changes. Plang supports any arity-2 functor for the element (or no arity-2 functor) whereas SWI-Prolog mandates the use of (-)/2. Plang's version is backwards compatible with SWI-Prolog's.
 
- See Also
 - (@<)/2, keysortd/2, rkeysort/2, sort/2
 
  keysortd/2 - sorts a keyed list into descending order.
- Usage
 - keysortd(List, Sorted)
 
- Description
 - Unifies Sorted with a sorted version of List. The elements should be functors of arity 2. The first argument to the functor is used as a key to be ordered according to (@>)/2. The tail of List can be a variable; the sorted version will have nil as its tail. 
 
- If an element is not a functor of arity 2, then the element itself will be used as the key.
 
- Errors
 
instantiation_error - List is a variable.  
type_error(list, List) - List is not a list or the tail of List is not nil or a variable. 
- Examples
  keysortd([a - 1, y - 2, b - 8, m - 6], [y - 2, m - 6, b - 8, a - 1])
- See Also
 - (@>)/2, keysort/2, rkeysortd/2, sortd/2
 
  msort/2 - sorts a list into ascending order without removing duplicates.
- Usage
 - msort(List, Sorted)
 
- Description
 - Unifies Sorted with a sorted version of List. The elements are ordered according to (@<)/2. The tail of List can be a variable; the sorted version will have nil as its tail.
 
- Errors
 
instantiation_error - List is a variable.  
type_error(list, List) - List is not a list or the tail of List is not nil or a variable. 
- Examples
  msort([a, y, b, m], [a, b, m, y])
 msort([a, y, a, m], [a, a, m, y])
- Compatibility
 - SWI-Prolog
 
- See Also
 - (@<)/2, keysort/2, msortd/2, sort/2
 
  msortd/2 - sorts a list into descending order without removing duplicates.
- Usage
 - msortd(List, Sorted)
 
- Description
 - Unifies Sorted with a sorted version of List. The elements are ordered according to (@>)/2. The tail of List can be a variable; the sorted version will have nil as its tail.
 
- Errors
 
instantiation_error - List is a variable.  
type_error(list, List) - List is not a list or the tail of List is not nil or a variable. 
- Examples
  msortd([a, y, b, m], [y, m, b, a])
 msortd([a, y, a, m], [y, m, a, a])
- See Also
 - (@>)/2, keysortd/2, msort/2, sortd/2
 
  rkeysort/2 - sorts a keyed list into ascending order with reversed keying.
- Usage
 - rkeysort(List, Sorted)
 
- Description
 - Unifies Sorted with a sorted version of List. The elements should be functors of arity 2. The second argument to the functor is used as a key to be ordered according to (@<)/2. The tail of List can be a variable; the sorted version will have nil as its tail. 
 
- If an element is not a functor of arity 2, then the element itself will be used as the key.
 
- Errors
 
instantiation_error - List is a variable.  
type_error(list, List) - List is not a list or the tail of List is not nil or a variable. 
- Examples
  rkeysort([a - 1, y - 2, b - 8, m - 6], [a - 1, y - 2, m - 6, b - 8])
- See Also
 - (@<)/2, keysortd/2, rkeysort/2, sort/2
 
  rkeysortd/2 - sorts a keyed list into descending order with reversed keying.
- Usage
 - rkeysortd(List, Sorted)
 
- Description
 - Unifies Sorted with a sorted version of List. The elements should be functors of arity 2. The second argument to the functor is used as a key to be ordered according to (@>)/2. The tail of List can be a variable; the sorted version will have nil as its tail. 
 
- If an element is not a functor of arity 2, then the element itself will be used as the key.
 
- Errors
 
instantiation_error - List is a variable.  
type_error(list, List) - List is not a list or the tail of List is not nil or a variable. 
- Examples
  rkeysortd([a - 1, y - 2, b - 8, m - 6], [b - 8, m - 6, y - 2, a - 1])
- See Also
 - (@>)/2, keysortd/2, rkeysort/2, sortd/2
 
  sort/2 - sorts a list into ascending order and remove duplicates.
- Usage
 - sort(List, Sorted)
 
- Description
 - Unifies Sorted with a sorted version of List. The elements are ordered according to (@<)/2. The tail of List can be a variable; the sorted version will have nil as its tail. Duplicate elements in List will appear only once in Sorted 
 
- Errors
 
instantiation_error - List is a variable.  
type_error(list, List) - List is not a list or the tail of List is not nil or a variable. 
- Examples
  sort([a, y, b, m], [a, b, m, y])
 sort([a, y, a, m], [a, m, y])
- Compatibility
 - SWI-Prolog
 
- See Also
 - (@<)/2, keysort/2, msort/2, sortd/2
 
  sortd/2 - sorts a list into descending order and remove duplicates.
- Usage
 - sortd(List, Sorted)
 
- Description
 - Unifies Sorted with a sorted version of List. The elements are ordered according to (@>)/2. The tail of List can be a variable; the sorted version will have nil as its tail. Duplicate elements in List will appear only once in Sorted 
 
- Errors
 
instantiation_error - List is a variable.  
type_error(list, List) - List is not a list or the tail of List is not nil or a variable. 
- Examples
  sortd([a, y, b, m], [y, m, b, a])
 sortd([a, y, a, m], [y, m, a])
- See Also
 - (@>)/2, keysortd/2, msortd/2, sort/2