Skip to content

Modern OrderedMap OrderedMap

Paul Squires edited this page Oct 19, 2026 · 5 revisions

Home › OrderedMap › OrderedMap › OrderedMap

OrderedMap

Holds key and value pairs kept sorted by key.

Syntax

OrderedMap( of TK, TV )

Notes

  • for each walks it in ascending key order, always -- which is the entire reason to reach for it over containers.Map.
  • The ordering contract is < on TK and nothing else: no HashOf and no = are needed. Two keys are the same key when neither is less than the other, which is what std::map means by equivalence.
  • containers.Map unless you need the order: it is O(1) per operation against this one's O(log n), one allocation instead of one per entry, and no pointer chasing.
  • Reading through m[ k ] inserts, exactly as in containers.Map.

Examples

#import "std/containers"
using containers

dim ages as OrderedMap( of ustring, integer )
ages.Put( "bob", 41 )
ages.Put( "ada", 36 )

'' ASCENDING KEY ORDER, always -- the reason to pay O(log n)
for each kv in ages
    print kv.key             '' ada / bob
next

'' Keys( ) and Values( ) come out sorted too, so they need no sort after
dim ks as Array( of ustring ) = ages.Keys( )
print ks[ 0 ]                '' ada

Library: OrderedMap
Category: OrderedMap
Documented under: Members

Clone this wiki locally