sort-numbers.js
Sort numbers in ascending or descending order
; //=> [-2, 0.45, Infinity]sortNumbers; //=> [Infinity, 0.45, -2]
Installation
Package managers
npm
npm install sort-numbers
Bower
bower install sort-numbers
Duo
var sortNumbers = ;
Standalone
Download the script file directly.
API
sortNumbers(numbers)
numbers: Array
of Number
without NaN
Return: Array
of Number
It returns the array numerically sorted in ascending order.
Note that Array.prototype.sort
works as lexical sort by default.
var arr = 100 -2 -Infinity; ; //=> [ -Infinity, -2, 100 ]arr; //=> [ -2, -Infinity, 100 ]
It returns an empty array when the argument is an empty array.
It throws a TypeError when the array contains non-number values or NaN
.
; //=> [] ; // throw a type error; // throw a type error
sortNumbers.desc(numbers)
numbers: Array
of Number
without NaN
Return: Array
of Number
It returns the array numerically sorted in descending order.
var arr = 0 1 2 3; sortNumbers //=> [3, 2, 1, 0] //=> [0, 1, 2, 3]
sortNumbers.asc(numbers)
An alias to sortNumbers
.
CLI
You can use this module as a CLI tool by installing it globally.
npm install -g sort-numbers
Usage
Usage: sort-numbers <number0> [<number1> <number2> ...] Options:--desc, -d Sort numbers in descending order --help, -h Print usage information--version, -v Print version
Example
sort-numbers -23 7 -Infinity Infinity
yields:
-Infinity,-23,7,Infinity
License
Copyright (c) 2014 - 2015 Shinnosuke Watanabe
Licensed under the MIT License.