🚀 DAY 2 OF LAUNCH WEEK: Unify Your Security Stack with Socket Basics.Learn more →
Socket
Book a DemoInstallSign in
Socket

sort-array

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sort-array

Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order

Source
npmnpm
Version
3.0.3
Version published
Maintainers
1
Created
Source

view on npm npm module downloads Build Status Dependency Status js-standard-style

sort-array

Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order.

const sortArray = require('sort-array')

Synopsis

Sort an array of primitives, for example strings.

> const partsOfTheDay = ['twilight', 'afternoon', 'morning', 'evening']

> sortArray(partsOfTheDay)
[ 'afternoon', 'evening', 'morning', 'twilight' ]

The default sort order is asc. You can also specify desc or the name of a property from the customOrders object. For example, sort parts of the day by the order in which they occur.

> sortArray(partsOfTheDay, {
  order: 'time',
  customOrders: {
    time: ['morning', 'afternoon', 'evening', 'twilight']
  }
})
[ 'morning', 'afternoon', 'evening', 'twilight' ]

Sort by a computed field, e.g. an algorithm to rank boxers by influence. Define your computed fields in the computed object, each value being a function which takes an array member as input and returns the value to be sorted by. In this example we sort by rank (the name of our computed field supplied in computed).

> const boxers = [
  { name: 'Amir', ticketsSold: 30000, titlesHeld: 2 },
  { name: 'Vasiliy', ticketsSold: 20000, titlesHeld: 4 },
  { name: 'Josh', ticketsSold: 10000, titlesHeld: 3 },
  { name: 'Anthony', ticketsSold: 90000, titlesHeld: 0 }
]

> sortArray(boxers, {
  by: 'rank',
  order: 'desc',
  computed: {
    rank: boxer => boxer.ticketsSold + (boxer.titlesHeld * 10000)
  }
})
[
  { name: 'Anthony', ticketsSold: 90000, titlesHeld: 0 },
  { name: 'Vasiliy', ticketsSold: 20000, titlesHeld: 4 },
  { name: 'Amir', ticketsSold: 30000, titlesHeld: 2 },
  { name: 'Josh', ticketsSold: 10000, titlesHeld: 3 }
]

You can use computed fields to sort by values deep in an object structure.

> const data = [
  { inner: { number: 2 } },
  { inner: { number: 3 } },
  { inner: { number: 1 } }
]

> sortArray(data, {
  by: 'number',
  computed: {
    number: row => row.inner.number
  }
})
[
  { inner: { number: 1 } },
  { inner: { number: 2 } },
  { inner: { number: 3 } }
]

Sort by multiple columns using multiple custom orders.

> const attributes = [
  { skill: 'accuracy', confidence: 'medium' },
  { skill: 'power', confidence: 'high' },
  { skill: 'speed', confidence: 'low' },
  { skill: 'power', confidence: 'low' },
  { skill: 'speed', confidence: 'high' },
  { skill: 'accuracy', confidence: 'low' },
  { skill: 'speed', confidence: 'medium' },
  { skill: 'accuracy', confidence: 'high' },
  { skill: 'power', confidence: 'medium' }
]

> sortArray(attributes, {
  by: ['skill', 'confidence'],
  order: ['skill', 'confidence'],
  customOrders: {
    skill: ['accuracy', 'speed', 'power'],
    confidence: ['low', 'medium', 'high'],
  }
})
[
  { skill: 'accuracy', confidence: 'low' },
  { skill: 'accuracy', confidence: 'medium' },
  { skill: 'accuracy', confidence: 'high' },
  { skill: 'speed', confidence: 'low' },
  { skill: 'speed', confidence: 'medium' },
  { skill: 'speed', confidence: 'high' },
  { skill: 'power', confidence: 'low' },
  { skill: 'power', confidence: 'medium' },
  { skill: 'power', confidence: 'high' }
]

sort-array

Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order.

Example

const sortArray = require('sort-array')

sortArray(array, [options]) ⇒ Array ⏏

Kind: Exported function
Returns: Array - Returns the array that was passed in.

ParamTypeDescription
arrayArrayThe input array to sort. It is sorted in place.
[options]objectSort options.
[options.by]Array.<string>One or more property names or computed fields to sort by. Specifying property names is only relevant when sorting an array of objects.
[options.order]Array.<string>One or more sort orders. Specify asc, desc or a property name from the options.customOrders object.
[options.customOrders]objectA dictionary object containing one or more custom orders. Each custom order value must be an array defining the order expected values must be sorted in.
[options.computed]objectA dictionary object containing one or more computed field functions.

Load anywhere

This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.

Node.js:

const arrayify = require('sort-array')

Within Node.js with ECMAScript Module support enabled:

import arrayify from 'sort-array'

Within an modern browser ECMAScript Module:

import arrayify from './node_modules/sort-array/dist/index.mjs'

Old browser (adds window.sortArray):

<script nomodule src="./node_modules/sort-array/dist/index.js"></script>

© 2015-19 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.

Keywords

sort

FAQs

Package last updated on 22 Oct 2019

Did you know?

Socket

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