---
# System prepended metadata

title: 'compare() ,  compareTo()'
tags: [java]

---

Sure. The `compare()`, `compareTo()`, `Comparator`, and `Comparable` interfaces are all used for sorting objects in Java. However, they are used in different ways and have different features.

* **`compare()`** is a method in the `Comparator` interface. It takes ==two objects of the same type== as its arguments and returns an integer. The return value of the `compare()` method indicates whether the first object is less than, equal to, or greater than the second object.
* **`compareTo()`** is a method in the `Comparable` interface. It takes ==an object of the same type== as the current object as its argument and returns an integer. The return value of the `compareTo()` method indicates whether the current object is less than, equal to, or greater than the object passed as an argument.
* **`Comparator`** is an interface that defines a custom ordering for a class. This means that the class can be sorted in a way that is not the natural ordering of the class. To implement Comparator, a class must override the `compare()` method.
* **`Comparable`** is an interface that defines a natural ordering for a class. This means that the class can be sorted in ascending or descending order without needing to write any additional code. To implement Comparable, a class must override the `compareTo()` method.

The main difference between `compare()` and `compareTo()` is that `compare()` takes two objects as its arguments, while `compareTo()` only takes one object. This is because the `compare()` method is used to compare two objects, while the `compareTo()` method is used to compare an object to itself.

:::success
The main difference between Comparator and Comparable is that Comparator defines a custom ordering for a class, while Comparable defines a natural ordering for a class.
:::
Comparable is typically used when the natural ordering of the class is sufficient. Comparator is used when the natural ordering of the class is not sufficient or when you want to sort the class in a way that is different from the natural ordering.

In terms of rows and columns, `compare()` and `compareTo()` can be used to sort objects in a row or column. The `Comparator` interface can be used to sort objects in multiple rows or columns.

Here is a table that shows the difference between `compare()`, `compareTo()`, `Comparator`, and `Comparable` in more detail:

| Feature | `compare()` | `compareTo()` | 
|---|---|---|
| Interface | `Comparator` | `Comparable` | `Comparator` | `Comparable` |
| Arguments | Two objects | One object | Two objects | One object |
| Return value | Integer | Integer | 
| Purpose | Compare two objects  , Define a custom ordering for a class| Compare an object to itself , Define a natural ordering for a class|  | |
| Natural ordering | No | Yes | No | Yes |
| Multiple rows/columns | No | No | Yes | Yes |

```
 @Override
  public int compare(Ball b1, Ball b2) {
    return b2.color.id < b1.color.id ? 1 : -1;
  }
  ```
  
  ```
   @Override
  public int compare(Ball b1, Ball b2) { // Comparator , 記
    // 1 -> means you want put b2 to left
    // -1 -> means you want put b1 to left
    if (b2.color == Color.YELLOW && b1.color == Color.YELLOW)
      return b2.id < b1.id ? 1 : -1;
    if (b2.color == Color.YELLOW)
      return 1;
    if (b1.color == Color.YELLOW)
      return -1;
    return b1.id > b2.id ? 1 : -1;
  }
  ```
  
  public static<T> void sort(List<T> list , Comparator<? super T> c ){list.sort( c );}

Comparator<? super T> c : 唔一定放個波入去，放parent都OK

