Interfaces

SortOption

The standard option for all the sortable fn

interface SortOption {
    desc?: boolean; //default: false, define is the sort must be ASC or DESC
    nullable?: boolean; // default: false, define is could be some null prop
}

SortByDateOption

This option is used for the byDate.

Inherits all the option of the SortOption and have the customParser to explicit how to parse a datable to a Date

import {datable, SortOption} from "sort-es";

interface SortByDateOption extends SortOption {
    customParser?: (item: datable) => Date;
}

Datable

This type defines all the possible types that a variable can take in order to be compatible with the conversion to Date

type datable = Date | number | string;

SortableTuple

This type defines an array that is composed of

[propertyToSort, relativeSortable]

the propertyToSort is the string containing the value of the property to be sorted,

the relativeSortable is the sortable to be used to sort that property.

import {sortable} from "sort-es";

export type GenericTuple<T> = [(item: T) => any, sortable<any>];
// AKA SortableTuple
export type ByValuesSorter<T, TTuple extends GenericTuple<T>[]> = [
    ...{
        [Key in keyof TTuple]: TTuple[Key] extends [
                (data: T) => infer U,
                sortable<infer X>
            ]
            ? [U] extends [X]
                ? TTuple[Key]
                : [TypeError, "Sortable type does not match with value provider."]
            : TTuple[Key];
    }
];

Example

import {byNumber, byString, byValues, SortableTuple} from "sort-es";

const currentUnsorted = [
    {prop: "ccc", att: 0},
    {prop: "aaa", att: 5},
    {prop: "aaa", att: 2},
    {prop: "ccc", att: 3},
    {prop: "ccc", att: 3},
    {prop: "bbb", att: 3},
];

const arraySorted = currentUnsorted.sort(
    byValues([
        [x => x.prop, byString()], //SortableTuple<string>
        [x => x.att, byNumber()], //SortableTuple<number>
    ])
);

SortableObject (DEPRECATED)

due to flaw design of javascript for in looping an object by property does not guarantee proper order. look at the linked issue to find out more.

This type defines an object that could have all the properties of the type that you want to sort, and as value, has a sortable of the type of the original value

import {sortable} from "sort-es";

type SortableObject<T> = {
    [key in keyof T]?: sortable<T[key]>
};

Example

import {byDate, byNumber, byString, ByValuesSorter} from "sort-es";

let byValueSorter: ByValuesSorter<{ name: string, id: number, birthDay: Date }>[];

//you create an object with a set of sortables of the type of the initial object
//the order of how the properties are entered is very important,
// the earlier it is written, the more important it is.
byValueSorter = [
    [x => x.id, byNumber()], //sortable<number>
    [x => x.name, byString()], //sortable<string>
    [x => x.birthDay, byDate()] //sortable<Date>
];

The order of how the properties are entered is very important, the earlier it is written, the more important it is.

In this case, the first comparison that is made is between the ids of the 2 objects, if it is the same, check the name and finally the birthDay