Compares objects using the compareTo function provided in the first object. If the 'compareTo' function is not available then it falls back to the DEFAULT_COMPARE.
Comparator
import { BY_COMPARE_TO, DEFAULT_COMPARE } from "apprt-core/comparators"class Sample { constructor({ id, msg = "" }) { this.id = id; this.msg = msg; } compareTo(other) { return DEFAULT_COMPARE(this.id, other?.id); } } const lotOfSamples = [new Sample({ id: "C" }), new Sample({ id: "F" }), new Sample({ id: "B" })]; lotOfSamples.sort(BY_COMPARE_TO); assert.deepEqual(["B", "C", "F"], lotOfSamples.map((s) => s.id)); Copy
import { BY_COMPARE_TO, DEFAULT_COMPARE } from "apprt-core/comparators"class Sample { constructor({ id, msg = "" }) { this.id = id; this.msg = msg; } compareTo(other) { return DEFAULT_COMPARE(this.id, other?.id); } } const lotOfSamples = [new Sample({ id: "C" }), new Sample({ id: "F" }), new Sample({ id: "B" })]; lotOfSamples.sort(BY_COMPARE_TO); assert.deepEqual(["B", "C", "F"], lotOfSamples.map((s) => s.id));
Compares objects using the compareTo function provided in the first object. If the 'compareTo' function is not available then it falls back to the DEFAULT_COMPARE.
Implements
Comparator
Example: _Usage with a class_