Socket
Socket
Sign inDemoInstall

sort-obj-array

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    sort-obj-array

Sort an array of objects using an intuitive object syntax


Version published
Maintainers
1
Install size
12.4 kB
Created

Readme

Source

sort-obj-array

NPM Version License Github Issues

Sort an array of objects using an intuitive object syntax

Features

  • Stable sorting
  • Nested sorting
  • No dependencies
  • Support for ImmutableJS

Install

npm install sort-obj-array

Usage

import sort, { inverse } from 'sort-obj-array';

const accounts = [
  { meta: { lastLogin: { day: 4 }, id: 7 }, type: 'user' },
  { meta: { lastLogin: { day: 6 }, id: 1 }, type: 'admin' },
  { meta: { lastLogin: { day: 2 }, id: 8 }, type: 'user' },
];

sort(accounts, {
  meta: {
    id: 1
  }
});

/* Returns:
[
  { meta: { lastLogin: { day: 6 }, id: 1 }, type: 'admin' },
  { meta: { lastLogin: { day: 4 }, id: 7 }, type: 'user' },
  { meta: { lastLogin: { day: 2 }, id: 8 }, type: 'user' },
]
*/

sort(accounts, inverse({
  meta: {
    id: 1
  }
}));

// Which is the same as...

sort(accounts, {
  meta: {
    id: -1
  }
});

/* Both returns:
[
  { meta: { lastLogin: { day: 2 }, id: 8 }, type: 'user' },
  { meta: { lastLogin: { day: 4 }, id: 7 }, type: 'user' },
  { meta: { lastLogin: { day: 6 }, id: 1 }, type: 'admin' },
]
*/

sort(accounts, {
  type: -1,       // Negative values imply a descending sort
  meta: {
    id: 2
  }
});

/* Returns:
[
  { meta: { lastLogin: { day: 4 }, id: 7 }, type: 'user' },
  { meta: { lastLogin: { day: 2 }, id: 8 }, type: 'user' },
  { meta: { lastLogin: { day: 6 }, id: 1 }, type: 'admin' },
]
*/

API Reference

sort(input : array, params : object) -> array

Sort an array of objects using the specified sorting parameters. Returns an array of objects which has been sorted.

Sorting parameters are specified with an object containing keys that correspond with which keys you want to sort and values that specify the ordering. Negative values imply a descending sort operation. The order of sorting operations is determined by abs(value).

inverse(params : object) -> object

A utility function for reversing the type of sorting operation from ascend to descend and vice-versa. This simply negates every value in params.

License

Contributing

Contributions are highly welcome and even encouraged!

Keywords

FAQs

Last updated on 29 Aug 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc