sort-unwind

v3.1.0
Sorts an array and then unwinds that sort on another array
functional array undo sort

Version Tests Coverage Status Downloads License

Sort Unwind

Sorts an array by an array of ranks and retains the stochastic tenet of the initial array so you can undo back to the original shuffled state.

Install

npm install --save sort-unwind

Usage

import { unwind } from 'sort-unwind'

const suits = ['♥', '♠', '♣', '♦']
const ranks = [2, 0, 3, 1]

const [sortedSuits, tenet] = unwind(ranks, suits)
// sortedSuits <- ['♠', '♦', '♥', '♣']
// tenet <- [1, 3, 0, 2]

You can then restore the entropy back into the array, or perhaps apply that entropy to another array. Sort of like sending that array backward in time to the more chaotic state.

const names = ['spades', 'diamonds', 'hearts', 'clubs']
const [tenetNames, tenetRanks] = unwind(tenet, names)
// tenetNames <- ['hearts', 'spades', 'clubs', 'diamonds']
// tenetRanks <- [2, 0, 3, 1]

Thanks

  • @varenc for showing me a clever way to do this.
  • @crosseye for working out a simpler way without a pipe
npm i sort-unwind

Metadata

  • MIT
  • >=18.0.0
  • Philihp Busby
  • released 7/20/2024

Downloads

Maintainers