Answer by Shiv Krishna Jaiswal for How to compare objects by multiple fields
I will suggest creating a static final attribute CMP and then use it in compareTo method,public class Person { private static final Comparator<Person> CMP =...
View ArticleAnswer by Abhilash Ranjan for How to compare objects by multiple fields
Java 8 through lambda way we can compare by method reference.Student POJOpublic class Student {int id;String firstName;String lastName;String subject;public Student(int id, String firstName, String...
View ArticleAnswer by pallgeuer for How to compare objects by multiple fields
Better late than never - if you're looking for unnecessary clutter or overhead then it's hard to beat the following in terms of least code/fast execution at the same time.Data class:public class MyData...
View ArticleAnswer by Sachchit Bansal for How to compare objects by multiple fields
Code implementation of the same is here if we have to sort the Person object based on multiple fields.import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;public class...
View ArticleAnswer by Pawan for How to compare objects by multiple fields
//Following is the example in jdk 1.8package com;import java.util.ArrayList;import java.util.Comparator;import java.util.List;class User { private String firstName; private String lastName; private...
View ArticleAnswer by Exodus for How to compare objects by multiple fields
It is easy to compare two objects with hashcode method in java`public class Sample{ String a=null; String b=null; public Sample(){ a="s"; b="a"; } public Sample(String a,String b){ this.a=a; this.b=b;...
View ArticleAnswer by Gerold Broser for How to compare objects by multiple fields
Starting from Steve's answer the ternary operator can be used:public int compareTo(Person other) { int f = firstName.compareTo(other.firstName); int l = lastName.compareTo(other.lastName); return f !=...
View ArticleAnswer by vaquar khan for How to compare objects by multiple fields
Following blog given good chained Comparator example http://www.codejava.net/java-core/collections/sorting-a-list-by-multiple-attributes-exampleimport java.util.Arrays;import...
View ArticleAnswer by Luke Machowski for How to compare objects by multiple fields
For those able to use the Java 8 streaming API, there is a neater approach that is well documented here:Lambdas and sortingI was looking for the equivalent of the C# LINQ:.ThenBy(...)I found the...
View ArticleAnswer by Xeroiris for How to compare objects by multiple fields
Another option you can always consider is Apache Commons. It provides a lot of options.import org.apache.commons.lang3.builder.CompareToBuilder;Ex:public int compare(Person a, Person b){ return new...
View ArticleAnswer by Display Name for How to compare objects by multiple fields
With Java 8: Comparator.comparing((Person p)->p.firstName) .thenComparing(p->p.lastName) .thenComparingInt(p->p.age);If you have accessor methods:Comparator.comparing(Person::getFirstName)...
View ArticleAnswer by Pradeep Singh for How to compare objects by multiple fields
//here threshold,buyRange,targetPercentage are three keys on that i have sorted my arraylist final Comparator<BasicDBObject> sortOrder = new Comparator<BasicDBObject>() { public int...
View ArticleAnswer by Benny Bottema for How to compare objects by multiple fields
(from Ways to sort lists of objects in Java based on multiple fields)Working code in this gistUsing Java 8 lambda's (added April 10, 2019)Java 8 solves this nicely by lambda's (though Guava and Apache...
View ArticleAnswer by Ran Adler for How to compare objects by multiple fields
import com.google.common.collect.ComparisonChain;/** * @author radler * Class Description ... */public class Attribute implements Comparable<Attribute> { private String type; private String...
View ArticleAnswer by missingfaktor for How to compare objects by multiple fields
Writing a Comparator manually for such an use case is a terrible solution IMO. Such ad hoc approaches have many drawbacks: No code reuse. Violates DRY.Boilerplate.Increased possibility of errors.So...
View ArticleAnswer by Andrejs for How to compare objects by multiple fields
Its easy to do using Google's Guava library.e.g. Objects.equal(name, name2) && Objects.equal(age, age2) && ...More examples:https://stackoverflow.com/a/5039178/1180621
View ArticleAnswer by Nigel_V_Thomas for How to compare objects by multiple fields
@Patrick To sort more than one field consecutively try ComparatorChainA ComparatorChain is a Comparator that wraps one or more Comparators in sequence. The ComparatorChain calls each Comparator in...
View ArticleAnswer by Boune for How to compare objects by multiple fields
You can also have a look at Enum that implements Comparator.http://tobega.blogspot.com/2008/05/beautiful-enums.htmle.g.Collections.sort(myChildren, Child.Order.ByAge.descending());
View ArticleAnswer by Steve Kuo for How to compare objects by multiple fields
You should implement Comparable <Person>. Assuming all fields will not be null (for simplicity sake), that age is an int, and compare ranking is first, last, age, the compareTo method is quite...
View ArticleAnswer by Mark Renouf for How to compare objects by multiple fields
If you implement the Comparable interface, you'll want to choose one simple property to order by. This is known as natural ordering. Think of it as the default. It's always used when no specific...
View Article