JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 556
  • Score
    100M100P100Q111079F
  • License MIT

Simple module to sort an array of objects by a property without mutating the original array.

Package Exports

  • sort-objects-array

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sort-objects-array) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sort-objects-array

This tiny and fast module performs the sort of an Array of objects with the property required. The module returns a new copy of the array so the original is not mutated. The module doesn't have any external dependencies (only tape to run the tests)

Install

You can install with [npm]:

$ npm install --save sort-objects-array

Usage

The module requires two parameters: The array to sort and the property to use in the sorting:

// Example1: Sorting the countries by the name

const sortObjectsArray = require('sort-objects-array');
const countries = [{'name': 'Colombia', 'code': 'co', 'area': 1197411},
                   {'name': 'Argentina', 'code': 'ar', 'area': 2766890},
                   {'name': 'Brasil', 'code': 'br', 'area': 8511965}]

sortObjectsArray(countries, 'name');
// Returns
// [ { name: 'Argentina', code: 'ar', area: 2766890 },
//   { name: 'Brasil', code: 'br', area: 8511965 },
//   { name: 'Colombia', code: 'co', area: 1197411 } ]

The default order is ascending but it's possible to change the order to descending using 'desc' or 'reverse' keyword as a third parameter:

// Example2: Sorting the countries by the largest area

sortObjectsArray(countries, 'area', 'desc');
// Returns
// [ { name: 'Brasil', code: 'br', area: 8511965 },
//   { name: 'Argentina', code: 'ar', area: 2766890 },
//   { name: 'Colombia', code: 'co', area: 1197411 } ]

Running tests

You can run the tests and check the functionality of this module using:

$ npm install && npm test

License

Copyright © 2018, Juan Convers. Released under the MIT License.