sort-o
v1.7.1
Published
Sort Object Keys
Downloads
16
Readme
sort-o
Utility for your sorting needs.
Features
- Sort keys of an object.
- Sort arrays by length and value.
- TBD..
Installation
npm install --save sort-o
yarn add sort-o
Usage
Require
const sorto = require('sort-o');
sorto.sort(data);
ES6 Import
import { sort, sortOrder } from 'sort-o';
sort(data, sortOrder.ASC);
API
sorto.sort(data [, sortOrder])
Sort data as per the specified order.
- Suppports deep sorting for object keys.
sortOrder
| sortOrder | Type | Value | Description |
| ----------------------------- | -------- | ------------- | ---------------------------------- |
| sortOrder.ASC (default)
| string
| 'asc' | Sort in ascending order |
| sortOrder.DESC | string
| 'desc' | Sort in descending order |
| sortOrder.ASC_LENGTH | string
| 'asc_length' | Sort in ascending order by length |
| sortOrder.DESC_LENGTH | string
| 'desc_length' | Sort in descending order by length |
Example
Sort keys of an object.
import { sortKeys, sortOrder } from 'sort-o';
const input = {
a: 1,
c: {
b: 2,
c: 3,
a: 1
},
b: 2
};
sortKeys(input, sortOrder.ASC);
// => {
// a: 1,
// b: 2,
// c:{
// a: 1,
// b: 2,
// c: 3
// }
// }
Sort array of strings.
import { sort, sortOrder } from 'sort-o';
const input = ['dddd', 'bb', 'ccc', 'a'];
sort(input, sortOrder.LENGTH);
// => [
// 'a',
// 'bb',
// 'ccc',
// 'dddd'
// ]