• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

4.29 Built-in list operations
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Built-in list operations
          • is_list/1
          • memberchk/2
          • length/2
          • sort/2
          • sort/4
          • msort/2
          • keysort/2
          • predsort/3
    • Packages
Availability:built-in
[ISO]keysort(+List, -Sorted)
Sort a list of pairs. List must be a list of Key-Value pairs, 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.

The keysort/2 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).
ClioPatria (version V3.1.1-51-ga0b30a5)