Quick Take

import { strict as assert } from "assert";
import { rSort } from "ranges-sort";

// Ranges (see codsen.com/ranges/) are sorted:
    [2, 3],
    [9, 10, "bad grey wolf"],
    [1, 2],
    [1, 2],
    [2, 3],
    [9, 10, "bad grey wolf"],


rSort(arr, [opts])

In other words, this library gives you a function and you must feed an array into its first argument and also if you wish, you can feed a second argument, the Optional Options Object (bracket in [, opts] means "optional").

Input argument Type Obligatory? Description
arrOfRanges Array yes Array of zero or more arrays meaning natural number ranges (2 elements each)
opts Plain object no Optional options go here.

For example,

[ [5, 9], [5, 3] ] => [ [5, 3], [5, 9] ]

This library does not mutate the inputs. In theory, a function in JavaScript could mutate its arguments, but only if they are on an "object" primitive type (an array or a plain object, for example).

Options object

options object's key Type Obligatory? Default Description
strictlyTwoElementsInRangeArrays Boolean no false If set to true, all ranges must have two and only elements, otherwise error is thrown. For example, input being [ [1, 2, 'zzz'] ] would throw (3 elements), as well as [ ['a'] ] (1 element).
progressFn Function no null If a function is given, it will be called with natural number meaning percentage of the total work done. It's approximate and used in worker setups.

Output: Sorted input array. First, we sort by the first argument of each child range array, then by second.

Here is whole Optional Options Object in one place, with all defaults, in case you want to copy it:

strictlyTwoElementsInRangeArrays: false,
progressFn: null


See it in the monorepo opens in a new tab, on GitHub.


To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.


MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 ranges-push 6.0.1
Gather string index ranges
📦 ranges-apply 6.0.1
Take an array of string index ranges, delete/replace the string according to them
📦 ranges-merge 8.0.1
Merge and sort string index ranges
📦 ranges-offset 3.0.1
Increment or decrement each index in every range
📦 string-range-expander 3.0.1
Expands string index ranges within whitespace boundaries until letters are met
📦 ranges-ent-decode 5.0.1
Recursive HTML entity decoding for Ranges workflow
📦 ranges-invert 5.0.1
Invert string index ranges