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. private static class Song implements Comparable<Song>{
  11. public Song(int sno, String name, String artist) {
  12. this.sno = sno;
  13. this.name = name;
  14. this.artist = artist;
  15. }
  16. private Integer sno;
  17. private String name;
  18. private String artist;
  19.  
  20. public String toString()
  21. {
  22. return sno + "\t" + name + "\t" + artist;
  23. }
  24.  
  25. @Override
  26. public int compareTo(Song arg0) {
  27. // TODO Auto-generated method stub
  28. return 0;
  29. }
  30. public static final Comparator<Song> NAMEComparator = new Comparator<Song>(){
  31.  
  32. @Override
  33. public int compare(Song o1, Song o2) {
  34. return o1.name.compareTo(o2.name); // salary is also positive integer
  35. }
  36.  
  37. };
  38. public static final Comparator<Song> ARTISTComparator = new Comparator<Song>(){
  39.  
  40. @Override
  41. public int compare(Song o1, Song o2) {
  42. return o1.artist.compareTo(o2.artist); // salary is also positive integer
  43. }
  44.  
  45. };
  46.  
  47.  
  48.  
  49.  
  50. }
  51.  
  52. public static void main(String[] args) {
  53.  
  54. ArrayList<Song> list = new ArrayList<Song>();
  55. list.add(new Song(1, "The Best Of Me", "Bryan Adams"));
  56. list.add(new Song(2, "I'm Ready", "Bryan Adams"));
  57. list.add(new Song(3, "Cloud Number Nine", "Bryan Adams"));
  58.  
  59.  
  60. list.add(new Song(4, "Leave out", "Linkin Park"));
  61. list.add(new Song(5, "What I've Done", "Linkin Park"));
  62. list.add(new Song(6, "In the end", "Linkin Park"));
  63.  
  64. list.add(new Song(7, "Hey You", "Pink Floyd"));
  65. list.add(new Song(8, "Another Brick in the Wall", "Pink Floyd"));
  66. list.add(new Song(9, "Comfortably Numb", "Pink Floyd"));
  67. list.add(new Song(10, "Hey You", "Bryan Adams"));
  68. System.out.println("\nOriginal List:");
  69. for(Song s : list){
  70. System.out.println(s.toString());
  71. }
  72.  
  73.  
  74. list.sort(Song.ARTISTComparator);
  75. System.out.println("\nSorted by Artist");
  76. for(Song s : list){
  77. System.out.println(s.toString());
  78. }
  79.  
  80. list.sort(Song.NAMEComparator);
  81. System.out.println("\nSorted by Name");
  82. for(Song s : list){
  83. System.out.println(s.toString());
  84. }
  85.  
  86. }
  87. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
Original List:
1	The Best Of Me	Bryan Adams
2	I'm Ready	Bryan Adams
3	Cloud Number Nine	Bryan Adams
4	Leave out	Linkin Park
5	What I've Done	Linkin Park
6	In the end	Linkin Park
7	Hey You	Pink Floyd
8	Another Brick in the Wall	Pink Floyd
9	Comfortably Numb	Pink Floyd
10	Hey You	Bryan Adams

Sorted by Artist
1	The Best Of Me	Bryan Adams
2	I'm Ready	Bryan Adams
3	Cloud Number Nine	Bryan Adams
10	Hey You	Bryan Adams
4	Leave out	Linkin Park
5	What I've Done	Linkin Park
6	In the end	Linkin Park
7	Hey You	Pink Floyd
8	Another Brick in the Wall	Pink Floyd
9	Comfortably Numb	Pink Floyd

Sorted by Name
8	Another Brick in the Wall	Pink Floyd
3	Cloud Number Nine	Bryan Adams
9	Comfortably Numb	Pink Floyd
10	Hey You	Bryan Adams
7	Hey You	Pink Floyd
2	I'm Ready	Bryan Adams
6	In the end	Linkin Park
4	Leave out	Linkin Park
1	The Best Of Me	Bryan Adams
5	What I've Done	Linkin Park