Advertisement
desislava_topuzakova

05. Comparing Objects - Person

Oct 16th, 2020
1,710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. package ComparingObjects_05;
  2.  
  3. public class Person implements Comparable<Person> {
  4.     private String name;
  5.     private int age;
  6.     private String town;
  7.  
  8.     public Person(String name, int age, String town){
  9.         this.name = name;
  10.         this.age = age;
  11.         this.town = town;
  12.     }
  13.  
  14.  
  15.     @Override
  16.     public int compareTo(Person other) {
  17.         int result = this.name.compareTo(other.name);
  18.         if(result == 0){
  19.             result  = Integer.compare(this.age, other.age);
  20.         }
  21.         if(result == 0){
  22.             result = this.town.compareTo(other.town);
  23.         }
  24.  
  25.         return result;
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement