ByStr~Sort
Does sort your arrays of T object
following a verbose sortString
π
Getting started - install it
and require it
$ npm install -D bystr-sort
const { sort } = require( "bystr-sort" );
- define a sort string:
Assuming you have an array of
T
object like this:A valid sort string could beconst greetings : T[] = [ { order:14 , greet:"Hello" , level:"medium" , lang:"en" }, { order:3 , greet:"yo" , level:"familiar" , lang:"fr" }, { order:5 , greet:"selamat pagi" , level:"medium" , lang:"id" } ];
const sortString = ` by greet of a greater than b's then by level of a < than b's `;
- sort your
T[]
:const sorted : T[] = sort( greetings , sortString );
Sortstring
- it is a string used to sort your array of
T
objects. - It gives you the ability to set multiple sorts by using the
then
keyword at the end of a sort sentence. a sort sentence is matches this regex patternby {colA|colB|colC|...|colN} of a[|b] lower[|greater,<,>] than b[|a]'s [then]
- The
by
keyword starts the sentence anda's
orb's
ends it. Then is used to chain the next sort sentence - To invert the sort direction, you can play with keywords
lower, greater, >, <
but also witha, b
order in the sentence. So:by ... b ... > ... a's
is also valid. Because why not ?
Note
- further updates will allow the case insensitiveness of the whole sort sentence ...
π
Bonus there is a littel more ...
parameters
Assuming the signature for sort is like the following:
const sort = (
objectArray: any[] ,
sortString = "" ,
crashOnError = false ,
logfn = console.error
): any[]
-
crashOnError
-
When set to true : It gives you the abbility to throw an exception when your sort string is invalid. You will be prompted with hints to fix your problem.
-
When set to false : It will prompt the error in the console.
-
-
logfn Use this to check errors within your tests.