fork(1) 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. static class Item{
  11. String name;
  12. int birth;
  13. Item(String n, int b){
  14. name = n;
  15. birth = b;
  16. }
  17. public String toString(){
  18. return "["+name+", "+birth+"]";
  19. }
  20. }
  21.  
  22. public static void main (String[] args) throws java.lang.Exception
  23. {
  24. Item chopin = new Item("Chopin", 1810);
  25. Item mozart = new Item("Mozart", 1756);
  26. Item beethoven = new Item("Beethoven", 1770);
  27. Item[] items = new Item[]{chopin, mozart, beethoven};
  28.  
  29. Arrays.sort(items, new Comparator<Item>(){
  30. public int compare(Item a, Item b){
  31. return a.birth - b.birth;
  32. }
  33. });
  34.  
  35. for(Item it:items)
  36. System.out.println(it);
  37. }
  38. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
[Mozart, 1756]
[Beethoven, 1770]
[Chopin, 1810]