Pabl0o0

Untitled

Apr 4th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. public class Test implements Comparator {
  2.  
  3. private static Comparator _comparator;
  4. public Test(Comparator comparator){
  5. _comparator = comparator;
  6. }
  7.  
  8. public static Student[] sortByName(Student[] tablica){
  9. for (int i = 0;i<tablica.length;i++){
  10. for(int j = 0; j<tablica.length-i;j++){
  11. if(_comparator.compare(tablica[i], tablica[i+1])>0){
  12. Student tmp = tablica[i];
  13. tablica[i] = tablica[i+1];
  14. tablica[i+1] = tmp;
  15. }
  16. }
  17. }
  18. return tablica;
  19. }
  20.  
  21.  
  22.  
  23. public static void main(String[] args) {
  24. // TODO Auto-generated method stub
  25.  
  26. Student student0 = new Student(0, "Kowalski", "Jan", 5.5);
  27. Student student1 = new Student(1, "Kowal", "Basia", 4.5);
  28. Student student2 = new Student(2, "Źródło", "Maciej", 3.5);
  29. Student student3 = new Student(3, "Wysoka", "Marta", 4.0);
  30. Student student4 = new Student(4, "Konarek", "Dorota", 5.0);
  31. Student student5 = new Student(5, "Nowy", "Bartłomiej", 2.0);
  32.  
  33. int size = 6;
  34. Student[] tablica = new Student[size];
  35. tablica[0] = student0;
  36. tablica[1] = student1;
  37. tablica[2] = student2;
  38. tablica[3] = student3;
  39. tablica[4] = student4;
  40. tablica[5] = student5;
  41.  
  42. System.out.println("Tablica w kolejności wpisania");
  43. for(int i = 0;i<tablica.length;i++){
  44. System.out.println(tablica[i]);
  45. }
  46.  
  47. System.out.println(sortByName(tablica));
  48.  
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment