JSPM

@writetome51/get-in-numeric-order-by-property

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

Re-orders objects by the numeric value of a particular property in each.

Package Exports

  • @writetome51/get-in-numeric-order-by-property

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 (@writetome51/get-in-numeric-order-by-property) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

getInNumericOrderByProperty

getInNumericOrderByProperty(property, objects): any[]

Returns new array of objects re-ordered by the numeric value of property in each.
The property in each object must be type 'number' and must be a finite number.
The parameter property is a string that can include dot notation ( i.e, 'property.subproperty.subsubproperty' ) .
Does not modify passed objects.
NOTE: if any object in objects does not have the property, the function will error.

Examples

let players = [
    {team: 'mets', homeRuns: 15},
    {team: 'yankees', homeRuns: 25},
    {team: 'athletics', homeRuns: 5},
    {team: 'braves', homeRuns: 11},
    {team: 'padres', homeRuns: 1},
    {team: 'giants', homeRuns: 55},
    {team: 'royals', homeRuns: 2},
    {team: 'rangers', homeRuns: 10},
    {team: 'marlins', homeRuns: 5},
    {team: 'red sox', homeRuns: 70},
    {team: 'white sox', homeRuns: 15}
];

let sortedPlayers = getInNumericOrderByProperty('homeRuns', players);

/***************************
sortedPlayers:
[ 
    { team: 'padres', homeRuns: 1 },
    { team: 'royals', homeRuns: 2 },
    { team: 'athletics', homeRuns: 5 },
    { team: 'marlins', homeRuns: 5 },
    { team: 'rangers', homeRuns: 10 },
    { team: 'braves', homeRuns: 11 },
    { team: 'mets', homeRuns: 15 },
    { team: 'white sox', homeRuns: 15 },
    { team: 'yankees', homeRuns: 25 },
    { team: 'giants', homeRuns: 55 },
    { team: 'red sox', homeRuns: 70 } 
]
***************************/

players = [
    {name: 'joe', numbers: [10, 5, 20]},
    {name: 'todd', numbers: [7, 15, 9]},
    {name: 'rick', numbers: [1, 2, 19]},
    {name: 'nelly', numbers: [4, 3, 21]}
];
// sort by the second number in each numbers array:
sortedPlayers = getInNumericOrderByProperty('numbers.1', players);

/***************************
sortedPlayers:
[ 
    { name: 'rick', numbers: [ 1, 2, 19 ] },
    { name: 'nelly', numbers: [ 4, 3, 21 ] },
    { name: 'joe', numbers: [ 10, 5, 20 ] },
    { name: 'todd', numbers: [ 7, 15, 9 ] } 
]
***************************/

Installation

You must have npm installed first. Then, in the command line:

npm install @writetome51/get-in-numeric-order-by-property

Loading

// If using TypeScript:
import {getInNumericOrderByProperty} from '@writetome51/get-in-numeric-order-by-property';
// If using ES5 JavaScript:
var getInNumericOrderByProperty = 
    require('@writetome51/get-in-numeric-order-by-property').getInNumericOrderByProperty;