array-sort-micro

Nearly weightless module that sorts an array of objects with provided object-key.

Usage no npm install needed!

<script type="module">
  import arraySortMicro from 'https://cdn.skypack.dev/array-sort-micro';
</script>

README

array-sort-micro

Nearly weightless module that sorts an array of objects with provided object-key.

Build Status

Install

$ npm install --save array-sort-micro

Usage

var arraySortMicro = require('array-sort-micro');
var obj =
[
  {butts: 'maximum', hamsters: 19},
  {butts: 'angry', hamsters: 7},
  {butts: 'sneaky', hamsters: 42}
];

var newArray = arraySortMicro(obj, 'hamsters');
// sorting integers
/*
[
{butts: 'sneaky', hamsters: 42},
{butts: 'maximum', hamsters: 19},
{butts: 'angry', hamsters: 7}
]
*/

var newArray2 = arraySortMicro(obj, 'butts');
// sorting strings alphabetically
/*
[
{butts: 'angry', hamsters: 7},
{butts: 'maximum', hamsters: 19},
{butts: 'sneaky', hamsters: 42}
]
*/

API

arraySortMicro(array, key, predicate)

key

accepts string or integers

predicate

If the value of the key provided is an integer the output is returned in descending order. The opposite is true of values that are strings. Reverse the default order with the following syntax:

var testValue = arraySortMicro(obj, 'hamsters', 'asc');
//ascending integers
/*
[
{butts: 'angry', hamsters: 7},
{butts: 'maximum', hamsters: 19},
{butts: 'sneaky', hamsters: 42}
]
*/

var testValue2 = arraySortMicro(obj, 'butts', 'desc');
/*
[
{butts: 'sneaky', hamsters: 42},
{butts: 'maximum', hamsters: 19},
{butts: 'angry', hamsters: 7}
]
*/

License

MIT © Christopher Howard