Optional
collection: Iterable<[K, V]>Optional
comparator: ((a, b) => number)Optional
options: { Optional
comparator: ((a, b) => number)Optional
options: { Optional
comparator: ((a, b) => number)Optional
options: { Optional
comparator: ((a, b) => number)Optional
options: { ©2020 - 2024 Oraichain Foundation
Creates a new immutable
SortedMap
containing the entries of the provided collection-like. The keys will be sorted by the provided comparator.The comparator is a function that takes two arguments (a, b) and returns 0 if a and b are equal, returns positive number when a > b and returns negative number if a < b.
If comparator is undefined, the
defaultComparator
will be applied. This comparator is different than the default comparator used byCollection.sort
because more stringent comparison rules have to be applied to support all the types and marginal values like NaN or Infinity.The
defaultComparator
first determines equality by callingImmutable.is
, then compares the types of both values. If both values are of the same type, then it compares the values by using the javascript comparison operators > and <, but before that the objects, functions, and symbols are converted to string.The internal data structures will be created according to the given options. If options are undefined then
defaultOptions
will be applied.The default options are:
Currently the only type implemented is
btree
. It is a classic B-Tree structure with keys and values not only in leaves but also in internal nodes. The optionbtreeOrder
is defined as the maximum number of children that any internal nodes can have and also implies maximum number of entries (key/value pairs) in any node which is (btreeOrder
- 1). ThebtreeOrder
option can be changed in a constructor and can be any integer greater or equal 3.There are many ways to quickly and conveniently create a
SortedMap
by calling a constructor. Below are some examples.Create a
SortedMap
from any array of [K,V]:From a keyed sequence:
From other collections (List, Range, Set, Map):
Use a custom comparator (reverse order):
Change the
btreeOrder
option:You can also conveniently create a
SortedMap
within a chain of sequence operations: