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

0.0.1 • Public • Published

Priority Sort

priority-sort is a versatile utility that sorts an array of items based on a given priority order. This function supports sorting both primitive types and objects by allowing the extraction of keys from objects for comparison.

Installation

npm install priority-sort

Usage

import prioritySort from "priority-sort";

const result = prioritySort(
  [1, 4, 2, 3], // the array to be sorted
  [3, 2] // the priority
);
console.log(result); // output: [3, 2, 1, 4]

Note that elements not in the priority sequence will keep the original order.

Also, you can specify a function to determine how to compare objects.

import prioritySort from "./path/to/prioritySort";

const result = prioritySort(
  [{ value: 1 }, { value: 2 }, { value: 3 }],
  [2, 3, 1],
  (x) => x.value
);
console.log(result); // output: [{ value: 2 }, { value: 3 }, { value: 1 }]

Lisence

MIT

Dependents (0)

Package Sidebar

Install

npm i priority-sort

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

8.4 kB

Total Files

8

Last publish

Collaborators

  • littlehaker