issorted
Returns a boolean indicating if an input array is sorted.
Installation
$ npm install compute-issorted
For use in the browser, use browserify.
Usage
To use the module,
var issorted = ;
issorted( arr[, comparator] )
Returns a boolean
indicating if an input array
is sorted.
var bool = ;// returns false
By default, the function checks if the input array
is sorted in ascending order. To impose a different order, provide a comparator
function.
// Descending order...{return b - a;}var bool = ;// returns true
The comparator
function should behave the same as a comparator provided to arr.sort()
.
The comparator should take two arguments: a
and b
, where a
and b
are consecutive array
elements. If the comparator returns a numeric
value less than or equal to 0
, consecutive elements are considered sorted; otherwise, issorted
returns false
.
Examples
var issorted =shuffle = ;// Simulate some data...var data = 5 ;for var i = 0; i < datalength; i++data i = i;// Randomly shuffle the data and check if sorted...var bool;console;for var j = 0; j < 100; j++;bool = ;console;
To run the example code from the top-level application directory,
$ node ./examples/index.js
Notes
This function runs in linear time: O(N)
, where N
is the input array
length.
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
License
Copyright
Copyright © 2014. Athan Reines.