simple-sort
simple-sort
provides a straightforward collection of sorting methods and sort functions.
simple-sort
?
Why use - It provides both sorting methods and sort functions.
- It makes sorting by locale simple (no separate API to remember).
- It provides functionality via plain English method names rather than hard-to-remember parameters.
- It provides you a consistent sorting API, so you don't have to individually write all sorting methods yourself.
- It makes your code more readable and easily understandable, even by somebody unfamiliar with the
simple-sort
API.
Installation
$ npm install simple-sort
Usage
Import the library:
; // or var Sorter = require('simple-sort').default;
Create an instance:
const sorter = 'en';
Sorting methods:
// sample data arrayconst numArr = 231; // sort values in ascending ordersorter;// -> [1,2,3] // sort values in descending ordersorter;// -> [3,2,1] // sample arrayconst wordArr = 'Janeway' 'Picard' 'Cisco'; // sort by locale in ascending ordersorter;// -> ['Cisco','Janeway','Picard'] // sort by locale in descending ordersorter;// -> ['Picard','Janeway','Cisco'] // sample arrayconst objArr = num:2num:3num:1; // sort array of objects by property in ascending ordersorter;// -> [{num:1},{num:2},{num:3}] // sort array of objects by property in descending ordersorter;// -> [{num:3},{num:2},{num:1}] // sample arrayconst objStrArr = name:'Janeway'name:'Picard'name:'Cisco'; // locale sort array of objects by property in ascending ordersorter;// -> [{name:'Cisco'},{name:'Janway'},{name:'Picard'}] // locale sort array of objects by property in descending ordersorter;// -> [{name:'Picard'},{name:'Janway'},{name:'Cisco'}]
Function-returning methods:
Each of the previous sort methods are also available as sort functions to pass into Array.sort()
. All function-returning methods have a Func
suffix.
// sample data arrayconst numArr = 231; // sort values in asending ordernumArr;// numArr -> [1,2,3] // sort values in descending ordernumArr;// numArr -> [3,2,1] // sample arrayconst wordArr = 'Janeway' 'Picard' 'Cisco'; // sort by locale in ascending orderwordArr;// wordArr -> ['Cisco','Janeway','Picard'] // sort by locale in descending orderwordArr;// wordArr -> ['Picard','Janeway','Cisco'] // sample arrayconst objArr = num:2num:3num:1; // sort array of objects by property in ascending orderobjArr;// objArr -> [{num:1},{num:2},{num:3}] // sort array of objects by property in descending orderobjArr;// objArr -> [{num:3},{num:2},{num:1}] // sample arrayconst objStrArr = name:'Janeway'name:'Picard'name:'Cisco'; // locale sort array of objects by property in ascending orderobjStrArr;// objStrArr -> [{name:'Cisco'},{name:'Janway'},{name:'Picard'}] // locale sort array of objects by property in descending orderobjStrArr;// objStrArr -> [{name:'Picard'},{name:'Janway'},{name:'Cisco'}]
Docs
Documentation of all available methods can be found here.
Contributions
Contributions are welcome! If you have any issues and/or contributions you would like to make, feel free to file an issue and/or issue a pull request.
License
Apache License Version 2.0
Copyright (c) 2016 by Ryan Burgett.