Comparable
Definition
Comparable is an interface defining a strategy of comparing an object with other objects of the same type. This is called the class’s “natural ordering.”
- Can be passed to a sort method (such as
Collections.sortorArrays.sort) to allow precise control over the sort order. - Can also be used to control the order of certain data structures (such as
sorted setsorsorted maps), or to provide an ordering for collections of objects that don’t have a natural ordering - The sorting order is decided by the return value of the
compareTo()method.
Example
public class Player {
private int ranking;
private String name;
private int age;
// constructor, getters, setters
@Override
public int compareTo(Player otherPlayer) {
return Integer.compare(getRanking(), otherPlayer.getRanking());
}
}compareTo()- The sorting order is decided by the return value of
compareTo()- It returns a number indicating whether the object being compared is less than, equal to, or greater than the object being passed as an argument.
- The Integer.compare(x, y) returns -1 if x is less than y, 0 if they’re equal, and 1 otherwise.
- The sorting order is decided by the return value of
public static void main(String[] args) {
List<Player> footballTeam = new ArrayList<>();
Player player1 = new Player(59, "John", 20);
Player player2 = new Player(67, "Roger", 22);
Player player3 = new Player(45, "Steven", 24);
footballTeam.add(player1);
footballTeam.add(player2);
footballTeam.add(player3);
// Before Sorting : [John, Roger, Steven]
System.out.println("Before Sorting : " + footballTeam);
Collections.sort(footballTeam);
// After Sorting : [Steven, John, Roger]
System.out.println("After Sorting : " + footballTeam);
}Comparator
Definition
A Java interface used to define a custom sorting order for objects
- giving a set of instructions to a sorting method (like
Collections.sort()orArrays.sort()) on how to compare two objects when you need more flexibility than the object’s “natural” order - Example -
Studentobject- if a class of
Studentobjects has a “natural” order to sort by student ID, aComparatorlets you create separate, reusable rules to sort those same students by last name, GPA, or age, without changing theStudentclass itself
- if a class of
compare method
compare(T o1, T o2)⇒ the primary method you have to implement- compares two objects (
o1ando2) and returns an integer with the following meaning:- Negative integer:
o1should come beforeo2. - Zero:
o1ando2are equal in terms of sorting. - Positive integer: o1 should come after o2
- Negative integer:
- compares two objects (
class Student {
private String name;
private double gpa;
// Constructor, getters, and toString()...
public Student(String name, double gpa) {
this.name = name;
this.gpa = gpa;
}
// getter, setter
}Lambda
import java.util.ArrayList;
import java.util.List;
// ... inside a method
List<Student> students = new ArrayList<>();
students.add(new Student("Eve", 3.9));
students.add(new Student("Charlie", 4.0));
students.add(new Student("Alice", 3.5));
// Sort by GPA (highest first) using a lambda
students.sort((s1, s2) -> Double.compare(s2.getGpa(), s1.getGpa()));
// students list is now sorted: [Charlie (4.0), Eve (3.9), Alice (3.5)]you can initialize it like this
-
`Comparator<MyObject> ageComparator = (o1, o2) -> Integer.compare(o1.getAge(), o2.getAge());`
Other examples
Sorting by string length
String[] words = {"banana", "apple", "grape", "orange"};
Arrays.sort(words, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return Integer.compare(s1.length(), s2.length());
}
});
// [apple, grape, banana, orange]
System.out.println(Arrays.toString(words)); Sorting numbers by ascending order
Integer[] numbers = {5, 3, 8, 1, 2};
Arrays.sort(numbers, new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return b - a; // 내림차순으로 정렬
}
});
System.out.println(Arrays.toString(numbers)); // [8, 5, 3, 2, 1]By a certain character at a specific index
String[] words = {"sun", "bed", "car"};
Arrays.sort(words, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
int index = 1; // n번째 인덱스
char c1 = s1.charAt(index);
char c2 = s2.charAt(index);
return Character.compare(c1, c2);
}
});
System.out.println(Arrays.toString(words)); // [car, bed, sun]ㅈimport java.util.*;
class Solution {
public String[] solution(String[] strings, int n) {
Arrays.sort(strings, new Comparator<String>(){
@Override
public int compare(String s1, String s2){
if(s1.charAt(n) > s2.charAt(n)) return 1;
else if(s1.charAt(n) == s2.charAt(n)) return s1.compareTo(s2);
else if(s1.charAt(n) < s2.charAt(n)) return -1;
else return 0;
}
});
return strings;
}
}