compute-issorted

1.0.0 • Public • Published

issorted

NPM version Build Status Coverage Status Dependencies

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 = require( 'compute-issorted' );

issorted( arr[, comparator] )

Returns a boolean indicating if an input array is sorted.

var bool = issorted( [ 2, 6, 13, 5 ] );
// 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...
function comparator( a, b ) {
    return b - a;
}
 
var bool = issorted( [ 13, 6, 5, 2 ], comparator );
// 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 = require( 'compute-issorted' ),
    shuffle = require( 'compute-shuffle' );
 
// Simulate some data...
var data = new Array( 5 );
 
for ( var i = 0; i < data.length; i++ ) {
    data[ i ] = i;
}
 
// Randomly shuffle the data and check if sorted...
var bool;
console.log( 'Data\t\tSorted?' );
for ( var j = 0; j < 100; j++ ) {
    shuffle( data );
    bool = issorted( data );
    console.log( data.join( ',' )+'\t'+bool );
}

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

MIT license.


Copyright

Copyright © 2014. Athan Reines.

Package Sidebar

Install

npm i compute-issorted

Weekly Downloads

131

Version

1.0.0

License

none

Last publish

Collaborators

  • kgryte