• 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 by Collection.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 calling Immutable.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:

    {type: 'btree', btreeOrder: 33}
    

    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 option btreeOrder 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). The btreeOrder 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]:

    > const { SortedMap } = require('@oraichain/immutable');
    > let a=SortedMap([['a','A'], ['c','C'], ['z','Z'], ['u','U'], ['b','B']]);
    SortedMap { "a": "A", "b": "B", "c": "C", "u": "U", "z": "Z" }

    From a keyed sequence:

    > const { SortedMap, SortedSet, List, Map, Seq, Set } = require('@oraichain/immutable');

    > let seq=Seq({x:'X', c:'B', m:'M', anylabel:'K', f:'F'});
    Seq { "x": "X", "c": "B", "m": "M", "anylabel": "K", "f": "F" }

    > let b=SortedMap(seq);
    SortedMap { "anylabel": "K", "c": "B", "f": "F", "m": "M", "x": "X" }

    From other collections (List, Range, Set, Map):

    > const { SortedMap, List, Map, Range, Seq, Set } = require('@oraichain/immutable');
    > let list=List(['orange', 'apple', 'banana']);
    List [ "orange", "apple", "banana" ]
    > let c=SortedMap(list.toKeyedSeq());
    SortedMap { 0: "orange", 1: "apple", 2: "banana" }

    > c=SortedMap(list.toKeyedSeq().flip());
    SortedMap { "apple": 1, "banana": 2, "orange": 0 }

    > let range=Range(30, 0, 5);
    Range [ 30...0 by -5 ]
    > c=SortedMap(range.toKeyedSeq());
    SortedMap { 0: 30, 1: 25, 2: 20, 3: 15, 4: 10, 5: 5 }
    > c=SortedMap(range.toKeyedSeq().flip());
    SortedMap { 5: 5, 10: 4, 15: 3, 20: 2, 25: 1, 30: 0 }

    > let set=Set(['orange', 'apple', 'banana']);
    Set { "orange", "apple", "banana" }
    > c=SortedMap(set.toKeyedSeq());
    SortedMap { "apple": "apple", "banana": "banana", "orange": "orange" }

    > let map=Map({x:'X', c:'B', m:'M', anylabel:'K', f:'F'});
    Map { "x": "X", "c": "B", "m": "M", "anylabel": "K", "f": "F" }
    > c=SortedMap(map);
    SortedMap { "anylabel": "K", "c": "B", "f": "F", "m": "M", "x": "X" }

    Use a custom comparator (reverse order):

    > let reverseComparator=(a, b)=>(a > b ? -1 : a < b ? 1 : 0);
    > let d=SortedMap(list.toKeyedSeq().flip(), reverseComparator);
    SortedMap { "orange": 0, "banana": 2, "apple": 1 }

    // Create an empty map with custom comparator
    > d=SortedMap(undefined, reverseComparator);

    Change the btreeOrder option:

    > let options={type: 'btree', btreeOrder: 17};
    > let e=SortedMap(list.toKeyedSeq().flip(), reverseComparator, options);
    SortedMap { "orange": 0, "banana": 2, "apple": 1 }

    // Create an empty set with default comparator and custom options
    > e=SortedMap(undefined, undefined, options);

    You can also conveniently create a SortedMap within a chain of sequence operations:

    > seq.toSortedMap();
    SortedMap { "anylabel": "K", "c": "B", "f": "F", "m": "M", "x": "X" }

    > seq.toSortedMap(reverseComparator);
    SortedMap { "x": "X", "m": "M", "f": "F", "c": "B", "anylabel": "K" }

    Type Parameters

    • K

    • V

    Parameters

    • Optional collection: Iterable<[K, V]>
    • Optional comparator: ((a, b) => number)
        • (a, b): number
        • Parameters

          • a: K
          • b: K

          Returns number

    • Optional options: {
          btreeOrder: number;
          type: string;
      }
      • btreeOrder: number
      • type: string

    Returns SortedMap<K, V>

  • Type Parameters

    • T

    Parameters

    • collection: Iterable<Iterable<T>>
    • Optional comparator: ((a, b) => number)
        • (a, b): number
        • Parameters

          • a: T
          • b: T

          Returns number

    • Optional options: {
          btreeOrder: number;
          type: string;
      }
      • btreeOrder: number
      • type: string

    Returns SortedMap<T, T>

  • Type Parameters

    • V

    Parameters

    • obj: {
          [key: string]: V;
      }
      • [key: string]: V
    • Optional comparator: ((a, b) => number)
        • (a, b): number
        • Parameters

          • a: string
          • b: string

          Returns number

    • Optional options: {
          btreeOrder: number;
          type: string;
      }
      • btreeOrder: number
      • type: string

    Returns SortedMap<string, V>

  • Type Parameters

    • K

    • V

    Parameters

    • Optional comparator: ((a, b) => number)
        • (a, b): number
        • Parameters

          • a: K
          • b: K

          Returns number

    • Optional options: {
          btreeOrder: number;
          type: string;
      }
      • btreeOrder: number
      • type: string

    Returns SortedMap<K, V>

Generated using TypeDoc