Skip to content

MikeWillisFC/multi-column-sort

 
 

Repository files navigation

multi-column-sort

Tiny, zero-dependency multi column sorting helper.

npm CircleCI Codecov GitHub

Installation

With npm:

$ npm i multi-column-sort

or with yarn:

$ yarn add multi-column-sort

Usage

import multiColumnSort from 'multi-column-sort'

const data = [
  { firstName: 'Richards', lastName: 'Shepard', balance: '$2,657.70' },
  { firstName: 'Bessie', lastName: 'Henry', balance: '$3,202.83' },
  { firstName: 'Richards', lastName: 'Hammond', balance: '$3,441.19' },
  { firstName: 'Allison', lastName: 'Church', balance: '$1,616.60' },
  { firstName: 'Bennett', lastName: 'Ferrell', balance: '$1,165.54' }
]

const getColumnValue = (column, value) => {
  switch (column) {
    case 'balance':
      return parseFloat(value.replace(/\,|\$/g, ''))
    default:
      return value
  }
}

const sorted = multiColumnSort(
  data,
  [
    ['firstName', 'ASC'],
    ['balance', 'DESC']
  ],
  getColumnValue
)

/* Or: */

const sorted = multiColumnSort(
  data,
  {
    firstName: 'ASC',
    balance: 'DESC'
  },
  getColumnValue
)

/* sorted: [
  { firstName: 'Allison', lastName: 'Church', balance: '$1,616.60' },
  { firstName: 'Bennett', lastName: 'Ferrell', balance: '$1,165.54' },
  { firstName: 'Bessie', lastName: 'Henry', balance: '$3,202.83' },
  { firstName: 'Richards', lastName: 'Hammond', balance: '$3,441.19' },
  { firstName: 'Richards', lastName: 'Shepard', balance: '$2,657.70' }
] */

API

multiColumnSort(array, sortArrayOrObject, getColumnValue)

Parameters

  • array array Array of objects to be sorted.
  • sortArrayOrObject array or object Array of tuples or object defining columns to be sorted by, order and direction e.g. [['name', 'ASC'], ['city', 'DESC']] or { name: 'ASC', city: 'DESC' }.
  • getColumnValue function Optional, by default all values are cast to string. Takes column and value arguments, must return value for comparison.

About

Tiny multi column sorting JS helper

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 97.2%
  • JavaScript 1.6%
  • Shell 1.2%