Skip to content
Quiz · Collections and generics · lesson 12 of 13

Quiz — compareTo and sorting

What does the program print?

JAVA
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        P[] ps = {new P("A", 2), new P("C", 1), new P("B", 3)};
        Arrays.sort(ps);
        for (P p : ps) { System.out.print(p.name()); }
    }
}
record P(String name, int age) implements Comparable<P> {
    public int compareTo(P o) { return Integer.compare(age, o.age); }
}