A set of comparators written in TypeScript
npm install ts-comparators



reverse method. The reverse method will return a new ChainableComparator that does the same thing as the original one in the reverse order.``typescript`
let values = [2, 3, 1];
let comparator = new BasicComparator
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = [3, 2, 1]
`typescript`
interface Obj {
prop1: number,
prop2: string
}
let values = [
{ prop1: 1, prop2: 'xylophone' },
{ prop1: 1, prop2: 'baseball' },
{ prop1: 2, prop2: 'hello' }
];
let comparator = new PropertyComparator
.reverse()
.then(new ValueComparator
values.sort((value1, value2) => comparator.compare(value1, value2));
//values = [,
// { prop1: 2, prop2: 'hello' },
// { prop1: 1, prop2: 'baseball' },
// { prop1: 1, prop2: 'xylophone' }
//];
is an abstract class that adds the ability to do chaining and reversing of the other comparators.$3
The BasicComparator is the simplest of all the comparators and doesn't do anything special except that it can be used internally by other comparators and possesses the ChainableComparator functionality.`typescript
let values = [3, 2, 1];
let comparator = new BasicComparator();
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = [1, 2, 3]
`$3
The StringComparator is basically a wrapper around Intl.collator for it to work with the rest of the comparators in this package.`typescript
let values = ['one', 'two', 'three'];
let comparator = new StringComparator();
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = ['one', 'three', 'two']
`$3
The PropertyComparator is used to compare objects by a specific property in them. It takes the name of the property to use for the comparison as the first parameter and comparator to perform the comparison as the second parameter.`typescript
interface Obj {
prop: number;
}
let values: Obj[] = [{prop: 2}, {prop: 3}, {prop: 1}];
let comparator = new PropertyComparator('prop', new BasicComparator());
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = [{prop: 1}, {prop: 2}, {prop: 3}]
`$3
The ValueComparator is one of the most powerful comparators because it allows you to transform the value prior to comparison or it can simply by used to pluck values out of an object. It takes function that transforms the value to use for the comparison as the first parameter and comparator to perform the comparison as the second parameter.`typescript
interface Obj {
prop: number;
}
let values: Obj[] = [{prop: 2}, {prop: 3}, {prop: 1}];
let comparator = new ValueComparator(obj => obj.prop, new BasicComparator());
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = [{prop: 1}, {prop: 2}, {prop: 3}]
`$3
The CustomComparator is a utility comparator to convert a regular compare function in to a ChainableComparator.`typescript
let values = [2, 3, 1];
let comparator = new CustomComparator((value1, value2) => value1 - value2);
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = [1, 2, 3]
``