> For the complete documentation index, see [llms.txt](https://katxupa.gitbook.io/katxupa/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://katxupa.gitbook.io/katxupa/get-a-taste-of-katxupa/usage/compare.md).

# 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:

```typescript
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

```typescript
const number1 = 10;
const number2 = 5;
const numberComparison = number1.compareTo(number2);
console.log(numberComparison); 
// Output: 1 (Example output; actual output will vary)
```

#### String Comparison

```typescript
const string1 = "apple";
const string2 = "banana";
const stringComparison = string1.compareTo(string2);
console.log(stringComparison); 
// Output: -1 (Example output; actual output will vary)
```

#### Date Comparison

```typescript
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

```typescript
const comparator: Comparator<number> = { compare: (a, b) => a - b };
console.log(comparator.compare(3, 5)); 
// Output: -2
```

#### String Comparator

```typescript
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

```typescript
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

```typescript
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

```typescript
const bool1 = true;
const bool2 = false;
console.log(bool1.compareTo(bool2)); 
// Output: 1
```

#### Number Comparison

```typescript
const num1 = 42;
const num2 = 24;
console.log(num1.compareTo(num2)); 
// Output: 18
```

#### String Comparison

```typescript
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.
