Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ComparableDemo {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("AliceKevin", 20));
        students.add(new Student("BobScott", 18));
        students.add(new Student("CharlieEverett", 22));
        
      System.out.println("Before sorting:");
      for (Student student : students) {
          System.out.println(student);
      }
  
      // Sort students based on natural ordering (age)
      Collections.sort(students);
  
      System.out.println("\\nAfter sorting:");
      for (Student student : students) {
          System.out.println(student);
      }
  }
}

...