total-sorta
v1.0.1
Published
Abstracted helper functions, based on total order. Inspired by Sedgewick.
Downloads
6
Maintainers
Readme
Total Sorta
This is a small set of helper functions, designed to compare array elements. Usually, you'd use them in implementing a sorting algorithm.
I got the idea of wrapping total order into functions from Sedgewick & Wayne's book on Algorithms.
They're reliable, and hopefully the JS community will find them useful too.
Installation
$ npm install total-sorta
Usage
var ts = require('total-sorta');
API
compare(v, w)
Compares two items & returns a result. It's assumed that they have some common characteristic.
Like the <=>
(spaceship) operator in Ruby, Perl, PHP or Groovy, it returns:
-1
if the 1st item is<
the 2nd item1
if the 1st item is>
the 2nd item0
otherwise
less(v, w)
- Returns true if & only if item
v
is less thanw
- Returns false if the item
v
is greater thanw
- Returns false if the two items are equal
more(v, w)
- Returns true if & only if item
v
is greater thanw
- Returns false if the item
v
is less thanw
- Returns false if the two items are equal
sorted(arr, [option])
Returns true if your array, arr
, is sorted.
By default, it will check for ascending order. If you want descending order,
pass in the option, { descending: true }
, as 2nd parameter.
For what it's worth, !condition
This may seem obvious, but it wasn't to me at first. Flip the boolean result
with !
to check for 'less than or equal to' or 'greater than or equal to'.
!more(v, w)
is true ifv <= w
!less(v, w)
is true ifv >= w