Advertisement
apl-mhd

TimeComparator

Dec 25th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class Time implements Comparable<Time> {
  4.  
  5.  
  6. int hour, minute;
  7.  
  8. public Time(int hour, int minute) {
  9. this.hour = hour;
  10. this.minute = minute;
  11. }
  12.  
  13.  
  14. @Override
  15. public String toString() {
  16. return hour +":"+minute+" PM";
  17. }
  18.  
  19. @Override
  20. public int compareTo(Time o) {
  21.  
  22.  
  23. if(o.hour==hour){
  24. if (o.minute<minute)
  25. return 1;
  26. }
  27. if(hour>o.hour)
  28.  
  29. return 1;
  30.  
  31. else
  32. return -1;
  33. // return minute- o.minute;
  34. }
  35. }
  36.  
  37.  
  38.  
  39. package com.company;
  40.  
  41. import java.util.ArrayList;
  42. import java.util.Collections;
  43.  
  44. public class Main {
  45.  
  46. public static void main(String[] args) {
  47. // write your code here
  48.  
  49.  
  50. ArrayList<Time> ob =new ArrayList<>();
  51.  
  52. ob.add(new Time(11,5));
  53. ob.add(new Time(12,2));
  54. ob.add(new Time(12,3));
  55. ob.add(new Time(13,0));
  56. ob.add(new Time(12,1));
  57.  
  58.  
  59.  
  60. Collections.sort(ob);
  61.  
  62.  
  63. for (Time i:ob) {
  64.  
  65. System.out.println(i);
  66.  
  67. }
  68.  
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement