-
-
Save iyengarajay/56c141c25b41b11bf7c3a97180468909 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TeenageSorter implements Comparator<Teenager>{ | |
@Override | |
public int compare(Teenager t1, Teenager t2) { | |
if( t1.getAge() == t2.getAge()){ | |
return sortByPhoneBrand(t1.getPhoneBrand(),t2.getPhoneBrand()); | |
} | |
return t1.getAge() > t2.getAge()? 1 : -1; | |
} | |
private int sortByPhoneBrand(String phoneBrand1,String phoneBrand2){ | |
if(phoneBrand1 == null && phoneBrand2 == null){ | |
return 0; | |
} | |
if(phoneBrand1 == null && phoneBrand2 != null){ | |
return 1; | |
} | |
if(phoneBrand1 != null && phoneBrand2 == null){ | |
return -1; | |
} | |
return phoneBrand1.compareTo(phoneBrand2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment