Comparable Interface

Natural ordering

Comparable

Defines natural ordering.

Code Examples

Natural sorting

java
1
2class User implements Comparable<User> {
3    public int compareTo(User u) {
4        return this.id - u.id;
5    }
6}
7          

Use Cases

  • Sorting

Common Mistakes to Avoid

  • Incorrect compareTo logic

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In