sort-o
TypeScript icon, indicating that this package has built-in type declarations

1.7.1 • Public • Published

sort-o

npm Build Status npm

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'
//    ]

Package Sidebar

Install

npm i sort-o

Weekly Downloads

2

Version

1.7.1

License

MIT

Last publish

Collaborators

  • pratishshr