🟰Compare
Comparable Interface
The Comparable
interface represents an object that can be compared to another object of the same type. It provides a method compareTo
that returns a negative number if the current object is less than the other, zero if they are equal, or a positive number if the current object is greater than the other.
To make an object comparable it needs to implement the Comparable
interface:
class FooBar implements Comparable<FooBar>{
constructor(private readonly code:number) {
}
compareTo(other: FooBar): number {
return this.code - other.code;
}
}
const foo = new FooBar(1);
const bar = new FooBar(2);
foo.compareTo(bar);
// Output: -1
Examples:
Number Comparison
const number1 = 10;
const number2 = 5;
const numberComparison = number1.compareTo(number2);
console.log(numberComparison);
// Output: 1 (Example output; actual output will vary)
String Comparison
const string1 = "apple";
const string2 = "banana";
const stringComparison = string1.compareTo(string2);
console.log(stringComparison);
// Output: -1 (Example output; actual output will vary)
Date Comparison
const date1 = new Date("2023-01-01");
const date2 = new Date("2023-01-15");
const dateComparison = date1.compareTo(date2);
console.log(dateComparison);
// Output: -1 (Example output; actual output will vary)
Comparator Interface
The Comparator
interface represents a generic comparator function for comparing two elements of the same type. It provides a method compare
that returns a negative number if the first element is less than the second, a positive number if the first element is greater than the second, or zero if they are equal.
Examples:
Number Comparator
const comparator: Comparator<number> = { compare: (a, b) => a - b };
console.log(comparator.compare(3, 5));
// Output: -2
String Comparator
const stringComparator: Comparator<string> = { compare: (a, b) => a.localeCompare(b) };
console.log(stringComparator.compare("apple", "banana"));
// Output: -1
InlineComparator Interface
The InlineComparator
interface represents a comparator function that can be used to compare two objects. It is a simpler alternative to the Comparator
interface.
Examples:
Number InlineComparator
const numberComparator: InlineComparator<number> = (a, b) => a - b;
const numberComparison = numberComparator(10, 5);
console.log(numberComparison);
// Output: 5 (Example output; actual output will vary)
String InlineComparator
const stringComparator: InlineComparator<string> = (a, b) => a.localeCompare(b);
const stringComparison = stringComparator("apple", "banana");
console.log(stringComparison);
// Output: -1 (Example output; actual output will vary)
Prototype Extensions
Katxupa extends the prototypes of Number
, String
, and Boolean
to include a compareTo
method. This allows for convenient comparisons between primitive types.
Examples:
Boolean Comparison
const bool1 = true;
const bool2 = false;
console.log(bool1.compareTo(bool2));
// Output: 1
Number Comparison
const num1 = 42;
const num2 = 24;
console.log(num1.compareTo(num2));
// Output: 18
String Comparison
const str1 = 'apple';
const str2 = 'banana';
console.log(str1.compareTo(str2));
// Output: -1
These extensions enhance the native JavaScript types with a consistent and convenient way to perform comparisons.
Last updated