import java.util.*; class Student implements Comparable { Student (double gpa) { this.gpa = gpa; } double gpa; public String toString() { return "S(" + this.gpa + ")"; } public int compareTo(Student other) { return (int) (this.gpa - other.gpa); // wrong when the difference is less than 1 // we just want to return 1, 0 or -1 depending on whether the difference is > 0, < 0 or == 0 // so we could do this instead: double diff = this.gpa - other.gpa; and then // return diff == 0.0 ? 0 : (int) Math.round(diff / Math.abs(diff)); // correct but convoluted // even better: if (diff > 0) return 1; else if (diff < 0) return -1; else return 0; } } class One { public static void main(String[] args) { ArrayList a = new ArrayList(); a.add(new Student(2.3)); a.add(new Student(1.3)); a.add(new Student(3.9)); a.add(new Student(3.3)); a.add(new Student(2.6)); a.add(new Student(2.1)); System.out.println( a ); Collections.sort( a ); System.out.println( a ); } }