sort-by-array
TypeScript icon, indicating that this package has built-in type declarations

0.2.6 • Public • Published

sort-by-array

Sort an array of objects or sub-arrays using a reference list.

Quick example:

import sortByArray from 'sort-by-array';

const nameArray = [
  { name: 'Anne', order: { this: 'a' } },
  { name: 'Bob', order: { this: 'b' } },
  { name: 'Henry', order: { this: 'b' } },
  { name: 'Andrew', order: { this: 'd' } },
  { name: 'Jason', order: { this: 'c' } },
  { name: 'Thomas', order: { this: 'e' } },
  { name: 'Peter', order: { this: 'e' } },
];

const sortedObjectBy = ['b', 'e', 'e', 'b', 'a', 'd', 'c'];
const sortedNames = sortByArray(
  nameArray,
  sortingArray,
  'order.this',
  'exact',
);

const nestedArray = [
  ['Anne', '1'],
  ['Chris', '4'],
  ['Vero', '3'],
  ['Luca', 2],
  ['Masha', 2],
  ['Kosta', 2],
  ['Quang', 5],
];

const sortNestedBy = [5, '1', 2, '3', '4'];
const sortedArrays = sortByArray(nestedArray, sortNestedBy, 1);

Package Sidebar

Install

npm i sort-by-array

Weekly Downloads

3

Version

0.2.6

License

MIT

Unpacked Size

6.35 kB

Total Files

5

Last publish

Collaborators

  • kvooak