compare code and sort object

What if you have a specific scenario where you want to first sort by rank and then alphabetically if the ranks are same?

Post Views: 1 Any number of Comparator classes can be created to sort them differently as shown below. import java.util.Comparator;   public class JavaTechnologyComparator implements Comparator<JavaTechnology> {   @Override public int compare(JavaTechnology t1, JavaTechnology t2) {   //handle null values here Integer rank1 = t1.getRank(); Integer rank2 = t2.getRank(); int rankVal = rank1.compareTo(rank2); int nameVal = t1.getName().toLowerCase().compareTo(t2.getName().toLowerCase());   //if same rank, then sort by name if(rankVal == 0){ return nameVal; } //else sort by rank return rankVal ; } }…

Read More

Now, what if you have your own custom class like a Dog, Cat, etc instead of a library class like String, Integer, etc?

Post Views: 0 Here is an example of a JavaTechnology custom object that implements a default sorting logic based on the rank (i.e popularity). public class JavaTechnology implements Comparable<JavaTechnology>{   private String name; private int rank;   // popularity lower value means more popular   public JavaTechnology(String name, int rank){ this.name = name; this.rank = rank; }   //default implementation by rank alone @Override public int compareTo(JavaTechnology technology) { int rank1 = this.rank; int rank2 = technology.rank; if (rank1 > rank2){…

Read More

Is there anything wrong with the above code?

Post Views: 0 Yes, 2 things — firstly, the above sort is case sensitive, that is the uppercase takes priority over lowercase pushing ‘Java’ after ‘JSP’. Secondly, if the collection had any null values, it will throw a NullpointerException. These two issues can be rectified by providing a custom sorting implementation that ignores case and handles null values by pushing them to the end. The Collections class’s sort method takes a Comparator implementation as a second argument. In the code…

Read More

Can you write a simple program that compares two objects to return if they are equal or not? This method will be handy in defining your own equals( ) method.

Post Views: 1 This is usefull in domain or value object class to compare different object fields in an equals method public class DomainObject {   //protected because only inheriting domain classes can use it protected boolean isPropertyEqual(Object compare1, Object compare2) { // go here if compare1 is null, i.e. test cases 1 & 3 if (compare1 == null) { if (compare2 != null) { return false; } //go here if compare1 is not null, i.e. test cases 2 &…

Read More

Now, what if you have your own custom class like a Dog, Cat, etc instead of a library class like String, Integer, etc?

Post Views: 1 Here is an example of a JavaTechnology custom object that implements a default sorting logic based on the rank (i.e popularity). public class JavaTechnology implements Comparable<JavaTechnology>{   private String name; private int rank;   // popularity lower value means more popular   public JavaTechnology(String name, int rank){ this.name = name; this.rank = rank; }   //default implementation by rank alone @Override public int compareTo(JavaTechnology technology) { int rank1 = this.rank; int rank2 = technology.rank; if (rank1 > rank2){…

Read More