sort.sort-by-key

function

Library: Sorting (OMSORT)
Import : omsort.xmd

Declaration
define function sort.sort-by-key
   modifiable integer shelf-to-sort

define function sort.sort-by-key
   modifiable stream shelf-to-sort

define function sort.sort-by-key
   modifiable switch shelf-to-sort

Argument definitions

shelf-to-sort
is a keyed shelf of integers, streams or switches


Purpose

Use sort.sort-by-key to sort a shelf of integers, streams or switches by their keys in ascending order.

Requirements

shelf-to-sort must have a key for every item (else OmniMark Error 6052).

If shelf-to-sort is a stream shelf, then it must

This example sorts a list of integers by their keys:

  import "omsort.xmd" prefixed by sort.
  process
     local integer integer-test-shelf variable initial
        {1 with key "a", 
         2 with key "c", 
         3 with key "b"}
     sort.sort-by-key integer-test-shelf
     
     repeat over integer-test-shelf
       output "key " 
          || key of integer-test-shelf 
          ||  "; item " 
          || "%d(integer-test-shelf)" 
          || "%n"
     again

This example sorts a list of streams by their keys:

  import "omsort.xmd" prefixed by sort.
  process
     local stream my-stream variable initial
        {"foo" with key "a", 
         "bar" with key "c",
         "sna" with key "b",
         "try" with key ""}
  
     sort.sort-by-key my-stream
     
     repeat over my-stream
       output "key " 
          || key of my-stream
          || "; item " 
          || my-stream
          || "%n"
     again

This example sorts a list of switches by their keys:


  import "omsort.xmd" prefixed by sort.
  process
     local switch my-switch variable initial
        {true with key "a", 
         false with key "c",
         true with key "b"}
  
     sort.sort-by-key my-switch
     
     repeat over my-switch
       output "key " 
          || key of my-switch
          || "; item " 
       do when my-switch
          output "true"
       else
          output "false"
       done
       output "%n"
     again

Related Topics
Other Library Functions