Skip to content
Advertisement

Sorted (ordered) collection for JavaScript

I’m looking for sorted container for JavaScript.

I’m using C++ std::set, https://en.cppreference.com/w/cpp/container/set and try porting my code to JavaScript.

JavaScript Map is not ordered container. I need some ordered container.

I don’t need completely compatible container of std::set on C++. My requirements are

  1. Custom comparator support
  2. Automatically sorted
  3. Find the specific value. If value is not found, get the next value (insertion position value).
  4. Iterator increment/decrement operation (move to prev/next element)

Here is C++ code example that demonstrates my requirements: https://wandbox.org/permlink/wGnTvTPyOej4G9jo

JavaScript

I found the following containers:

collctions/sorted-set https://www.npmjs.com/package/sorted-btree

It satisfies 1, 2, and 3, but not support 4.

collctions/sorted-array-set http://www.collectionsjs.com/sorted-array-set

It satisfies 1, 2, and 4(maybe), but not support 3.

Does someone know any container that support my requirements?

Advertisement

Answer

js-sdsl

A javascript standard data structure library which benchmark against C++ STL.

This library has strict time complexity guarantee and can be used with confidence.

The latest beta version includes iterator functions which can be used like iterator in c++.

Included data structures

  • Vector
  • Stack
  • Queue
  • LinkList
  • Deque
  • PriorityQueue
  • Set (using RBTree)
  • Map (using RBTree)
  • HashSet (for reference only)
  • HashMap (for reference only)

Usage

To help you have a better use, we provide this API document.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement