@patrtorg/consectetur-culpa-non
Blazing fast, tree-shakeable, type-safe, modern utility library to sort any type of array
Docs : https://@patrtorg/consectetur-culpa-non.netlify.com/
Getting started
installation
The library is available as a npm package.
To install the package, run:
npm install @patrtorg/consectetur-culpa-non
# or
yarn add @patrtorg/consectetur-culpa-non
Start using:
import {byString} from '@patrtorg/consectetur-culpa-non'
const unsorted = ["xxx", "bbbb", "zzz", "cccc", "aaa"];
const sorted = unsorted.sort(byString());
console.log(sorted);
Use directly in the browser
<script src='https://cdn.jsdelivr.net/npm/@patrtorg/consectetur-culpa-non/dist/index.umd.js'></script>
<script>
const unsorted = ["xxx", "bbbb", "zzz", "cccc", "aaa"];
const sorted = unsorted.sort(sort.byString());
console.log(sorted);
</script>
//or via browser modules
<script type='module'>
import {byString} from 'https://cdn.jsdelivr.net/npm/@patrtorg/consectetur-culpa-non/dist/index.mjs'
const unsorted = ["xxx", "bbbb", "zzz", "cccc", "aaa"];
const sorted = unsorted.sort(byString());
console.log(sorted);
</script>
Some mind-blowing example
sort by a single property
import {byValue, byNumber, byString} from '@patrtorg/consectetur-culpa-non'
const arrayUnsorted = [
{prop: "xxx", foo: 34},
{prop: "aaa", foo: 325},
{prop: "zzz", foo: 15},
{prop: "ccc", foo: 340},
{prop: "bbb", foo: 0}
];
const sortedByFoo = arrayUnsorted.sort(byValue((i) => i.foo, byNumber()));
console.log(sortedByFoo);
const sortedByProp = arrayUnsorted.sort(byValue((i) => i.prop, byString({desc: true})));
console.log(sortedByProp);
sort by a multiple property
import {byNumber, byString, byValues} from "@patrtorg/consectetur-culpa-non";
const objsToSort = [
{id: 2, name: 'teresa'},
{id: 3, name: 'roberto'},
{id: 2, name: 'roberto'}
];
const sortedObject = objsToSort.sort(byValues([
[(x) => x.name, byString()],
[(x) => x.id, byNumber()]
]));
console.log(sortedObject);
const sortedObject2 = objsToSort.sort(byValues([
[(x) => x.id, byNumber()],
[(x) => x.name, byString()]
]));
console.log(sortedObject2);
const sortedObject3 = objsToSort.sort(byValues([
[(x) => x.id, byNumber()],
[(x) => x.name, byString({desc: true})],
]));
console.log(sortedObject3);
typescript types check
import {byValue, byNumber, byString} from '@patrtorg/consectetur-culpa-non'
const objsArray = [{numbProp: 2, stringProp: 'a'}, {numbProp: 3, stringProp: 'f'}];
const incorrectSortedArray = objsArray.sort(byValue(i => i.numbProp, byString()));
const sortedArray = objsArray.sort(byValue(i => i.numbProp, byNumber()))
See full Docs
License
MIT © Cosimo chellini