1.0.1 ā¢ Published 4 years ago
obj-sorter v1.0.1
ā³ Install
npm i obj-sorter
š Usage
Node JS
const { objSorter } = require('obj-sorter');
JavaScript
import { objSorter } from 'obj-sorter';
Full Example
/* CASE */
const testArray = [
{ name: 'Jason', height: 180, weight: 80 },
{ name: 'Rachel', height: 190, weight: 76 },
{ name: 'John', height: 160, weight: 50 },
{ name: 'AJ', height: 180, weight: 70 },
{ name: 'Duke', height: 190, weight: 70 },
{ name: 'Elon', height: 160, weight: 60 },
{ name: 'Fred', height: 180, weight: 70 },
{ name: 'Peng', height: 160, weight: 50 },
];
/* INTERFACE */
const sorted = objSorter(testArray)
.setPropUp('height')
// sort by "height" in ASCENDING (up) order.
.setPropDown('weight')
// if "height" are same, sort by "weight" in DESCENDING (down) order.
.setPropDown('name')
// if "height" and "weight" are same, sort by "name" in DESCENDING (down) order.
.sort(); // => call sort() at last
/* RESULT */
console.log(sorted);
[
{ name: 'Elon', height: 160, weight: 60 },
{ name: 'Peng', height: 160, weight: 50 },
{ name: 'John', height: 160, weight: 50 },
{ name: 'Jason', height: 180, weight: 80 },
{ name: 'Fred', height: 180, weight: 70 },
{ name: 'AJ', height: 180, weight: 70 },
{ name: 'Rachel', height: 190, weight: 76 },
{ name: 'Duke', height: 190, weight: 70 },
];
Author
š¤ Jason-sj
- Github: @seong7
- LinkedIn: Seongjin Kim
š¤ Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Show your support
Give a āļø if this project helped you!
š License
Copyright @2020 Seongjin Kim.
This project is MIT licensed.
This README was generated with ā¤ļø by readme-md-generator