fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static class CalendarDate implements Comparable<CalendarDate> {
  12. private int month;
  13. private int day;
  14.  
  15. public CalendarDate(int month, int day) {
  16. this.month = month;
  17. this.day = day;
  18. }
  19.  
  20. public int compareTo(CalendarDate other) {
  21. if (month != other.month) {
  22. return month - other.month;
  23. }
  24. else {
  25. return day - other.day;
  26. }
  27. }
  28.  
  29. public String toString() {
  30. return month + "/" + day;
  31. }
  32. }
  33.  
  34.  
  35. public static void main (String[] args) throws java.lang.Exception {
  36. ArrayList<CalendarDate> birthday = new ArrayList<CalendarDate>();
  37. birthday.add(new CalendarDate(1, 23));
  38. birthday.add(new CalendarDate(5, 18));
  39. birthday.add(new CalendarDate(12, 17));
  40. birthday.add(new CalendarDate(2, 29));
  41. birthday.add(new CalendarDate(8, 6));
  42.  
  43. System.out.println("birthdays = " + birthday);
  44. Collections.sort(birthday);
  45. System.out.println("birthdays = " + birthday);
  46. }
  47. }
  48.  
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
birthdays = [1/23, 5/18, 12/17, 2/29, 8/6]
birthdays = [1/23, 2/29, 5/18, 8/6, 12/17]