seq-ord

seq-ord generates the function (a, b) => 1 | 0 | -1 accepted by Array#sort() from complex rules.

Usage no npm install needed!

<script type="module">
  import seqOrd from 'https://cdn.skypack.dev/seq-ord';
</script>

README

seq-ord

seq-ord generates the function (a, b) => 1 | 0 | -1 accepted by Array#sort() from complex rules.

Examples

import seqOrd from 'seq-ord';

let sortedBooks = books.sort(seqOrd(function* (a, b, rules) {
  yield rules.binary(a.available, b.available);
  yield rules.text(a.title, b.title);
  yield rules.date(a.date, b.date);
}));
let sortedBooks = books.sort(seqOrd(function* (a, b, rules) {
  yield rules.binaryExt(
    rules.sequence(function* () { /* both are unavailable */ })
    rules.sequence(function* () { /* both are available */ })
  )(a.available, b.available);
}));

Rules

  • binary – false values first
  • binaryExt(<cmp func>, <cmp func>) – allows using different rules for !a && !b and a && b
  • date – older dates first
  • moment – treats arguments as Moment.js dates
  • numeric – smaller numbers first
  • text(., ., locales, options) – uses localeCompare(locales, options)
  • textCodepoint – uses < and >, lowest codepoint first
  • sequence(<generator>, <this>)