Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A stable array sort for JavaScript
JavaScript
tag: v0.1.3

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.gitignore
.npmignore
.travis.yml
README.md
package.json
stable.js
stable.min.js
test.js

README.md

Stable Build Status

A stable array sort, because Array#sort() is not guaranteed stable.

MIT licensed.

From the browser

Include stable.js or the minified version stable.min.js in your page, then call stable().

From Node.js

Install using NPM:

npm install stable

Require in your code:

var stable = require("stable");

Usage

The default sort is, as with Array#sort, lexicographical. Unlike Array#sort, sorting is NOT performed in-place.

stable(["foo", "bar", "baz"]);   // => ["bar", "baz", "foo"]
stable([10, 1, 5]);              // => [1, 10, 5]

A comparator function can be specified:

// Regular sort() compatible comparator, that returns a number.
// This demonstrates the default behavior.
function lexCmp(a, b) {
    return String(a).localeCompare(b);
}
stable(["foo", "bar", "baz"], lexCmp);   // => ["bar", "baz", "foo"]

// Boolean comparator. Sorts `b` before `a` if true.
// This demonstrates a simple way to sort numerically.
function greaterThan(a, b) {
    return a > b;
}
stable([10, 1, 5], greaterThan);  // -> [1, 5, 10]
Something went wrong with that request. Please try again.