• Creates a new immutable SortedSet containing the values of the provided collection-like. The values 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 SortedSet by calling a constructor. Below are some examples.

    Create a SortedSet from any array:

    > const { SortedMap } = require('@oraichain/immutable');
    > let a=SortedSet(['a', 'c', 'z', 'u', 'b', 'q', 'd']);
    SortedSet { "a", "b", "c", "d", "q", "u", "z" }

    From a sequence of values:

    > 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=SortedSet(seq.keySeq());
    SortedSet { "anylabel", "c", "f", "m", "x" }

    > let c=SortedSet(seq.valueSeq());
    SortedSet { "B", "F", "K", "M", "X" }

    From a string:

    > let d=SortedSet('abcdefg');
    SortedSet { "a", "b", "c", "d", "e", "f", "g" }

    > let e=SortedSet('gefdcba');
    SortedSet { "a", "b", "c", "d", "e", "f", "g" }

    > e=SortedSet("hello");
    SortedSet { "e", "h", "l", "o" }

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

    > let list=List(['orange', 'apple', 'banana']);
    List [ "orange", "apple", "banana" ]
    > let f=SortedSet(list);
    SortedSet { "apple", "banana", "orange" }

    > let range=Range(30, 0, 5);
    Range [ 30...0 by -5 ]
    > f=SortedSet(range);
    SortedSet { 5, 10, 15, 20, 25, 30 }

    > let set=Set(['orange', 'apple', 'banana']);
    Set { "orange", "apple", "banana" }
    > f=SortedSet(set);
    SortedSet { "apple", "banana", "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" }
    > f=SortedSet(map.keySeq());
    SortedSet { "anylabel", "c", "f", "m", "x" }

    Use a custom comparator (reverse order):

    > let reverseComparator=(a, b)=>(a > b ? -1 : a < b ? 1 : 0);
    > let g=SortedSet(list, reverseComparator);
    SortedSet { "orange", "banana", "apple" }

    // Create an empty set with custom comparator
    > g=SortedSet(undefined, reverseComparator);

    Change the btreeOrder option:

    > let options={type: 'btree', btreeOrder: 17};
    > let h=SortedSet(list, reverseComparator, options);
    SortedSet { "orange", "banana", "apple" }

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

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

    > seq.keySeq().toSortedSet();
    SortedSet { "anylabel", "c", "f", "m", "x" }

    > seq.valueSeq().toSortedSet();
    SortedSet { "B", "F", "K", "M", "X" }

    > seq.valueSeq().toSortedSet(reverseComparator);
    SortedSet { "X", "M", "K", "F", "B" }

    Type Parameters

    • T

    Parameters

    • Optional collection: 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 SortedSet<T>

  • Type Parameters

    • T

    Parameters

    • 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 SortedSet<T>

©2020 - 2024 Oraichain Foundation