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)
          .thenComparing(Person::getLastName)
          .thenComparingInt(Person::getAge);

If a class implements Comparable then such comparator may be used in compareTo method:

@Override
public int compareTo(Person o){
    return Comparator.comparing(Person::getFirstName)
              .thenComparing(Person::getLastName)
              .thenComparingInt(Person::getAge)
              .compare(this, o);
}
🌐
ZetCode
zetcode.com › java › comparablecomparator
Java Comparable Comparator - comparing objects with Comparable and Comparator
Java Comparable and Comparator tutorial shows how to compare objects in Java with Comparable and Comparator interfaces. Comparing two objects is essential when doing sorting. When working with custom Java objects to perform comparisons, we can use Comparable or Comparator interfaces.
🌐
HappyCoders.eu
happycoders.eu › java › comparator-comparable-compareto
compareTo, Comparable, Comparator - Comparing Objects in Java
June 12, 2025 - The most significant application for comparing two objects is certainly the sorting of object lists or arrays. To sort objects, the program has to compare them and find out if one object is smaller, larger, or equal to another. You can find the article's source code in this GitHub repository. You compare Java primitives (int, long, double, etc.) using the operators <, <=, ==, =>, >. That does not work for objects. For objects, you either use a compare or a compareTo method instead.
🌐
Baeldung
baeldung.com › home › java › core java › comparator and comparable in java
Comparator and Comparable in Java | Baeldung
March 26, 2025 - Now that we have a clear understanding ... directly implementing an interface. The Comparator interface defines a compare(arg1, arg2) method with two arguments that represent compared objects, and works similarly to the Comparable...
🌐
Baeldung
baeldung.com › home › java › core java › comparing objects in java
Comparing Objects in Java | Baeldung
October 10, 2025 - For more information, check out our article about this topic. Now let’s look at the Objects#equals static method. We mentioned earlier that we can’t use null as the value of the first object, otherwise a NullPointerException will be thrown. The equals() method of the Objects helper class solves that problem. It takes two arguments and compares them, also handling null values.
🌐
LabEx
labex.io › tutorials › java-how-to-compare-objects-with-custom-comparator-in-java-419620
How to compare objects with custom comparator in Java | LabEx
In Java, a Comparator is an interface that allows you to define custom comparison logic for objects. It provides a way to compare two objects and determine their order, which is particularly useful when you want to sort collections or implement custom sorting mechanisms.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Comparator.html
Comparator (Java Platform SE 8 )
4 days ago - Returns a comparator that compares Comparable objects in natural order. The returned comparator is serializable and throws NullPointerException when comparing null. ... Returns a null-friendly comparator that considers null to be less than non-null. When both are null, they are considered equal.
🌐
Medium
medium.com › @AlexanderObregon › javas-objects-compare-method-explained-7a9ccda5ea74
Java’s Objects.compare() Method Explained | Medium
December 13, 2024 - The Objects.compare() method in Java is a utility that simplifies the process of comparing two objects by utilizing a specified Comparator. It is particularly useful for handling scenarios where objects being compared may include null values. ...
Find elsewhere
Top answer
1 of 5
3

The “different member variables” is irrelevant. It’s an implementation detail. What you need is a set of rules which of two people comes first.

You could for example sort by family name, then given name, then date of birth, and if these are all three equal, take the name of the school, university or company (which will be different member variables) and compare them as strings. If that is equal, you might have student and employee ids, and the student ids might be unique, and the employee ids might be unique, but student and employee ids might be the same. So you could sort then students first ordered by id, followed by employees sorted by id, if you might sort by if first if student and employee ids are comparable.

(University or school and employer might be the same, because universities are also employers).

2 of 5
3

Comparing objects with different fields sounds like bad polymorphic design, whether it's Java or any other OOP language:

  • If your comparator needs to know the precise subtype of an object to do the comparison, you mess-up with the the open-closed principle, since for every new subclassing, you'd potentially need to modify the comparator to select the relevant fields.
  • If your comparator needs uses reflexion to find on its own the relevant fields to compare, you indirectly mess up with the principle of encapsulation, since you create a hidden requirement that information to be compared must be in some predetermined field.

If you want to sort People properly in a clean polymorphic design:

  • you need to rely either on a field, available for any kind of People, including Student, or
  • you may call some function/transformation that provides a unique value (e.g. a string) that allows to sort any People. People and Student may then just use a different transformation that will be passed to the comparator; Or
  • you only sort among homogeneous subtypes.
🌐
LabEx
labex.io › tutorials › java-how-to-compare-java-objects-based-on-multiple-attributes-417392
How to compare Java objects based on multiple attributes | LabEx
The Comparator<T> interface defines a method compare(T o1, T o2) that compares two objects of the same type. Unlike Comparable, which is implemented by the class being compared, a Comparator is a separate class or lambda expression that can ...
🌐
Coderanch
coderanch.com › t › 404400 › java › compare-objects-comparator
How to compare to objects using comparator? (Beginning Java forum at Coderanch)
4 weeks ago - FooComparator fc = ... if (fc.compare(aFoo, anotherFoo) > 0) ... The main reason Comparator is especially useful is that there is often more than one way to compare instances of a single class: you might sort Person objects by name, by address, by age... so you can define a NameComparator, an AddressComparator, and an AgeComparator. If you have Person implement Comparable, then you can only do it one way. A lot of the Java Collections classes let you supply a Comparator to tell the collection how to sort the objects you put into the collection.
🌐
Tutorialspoint
tutorialspoint.com › java › java_using_comparator.htm
Java - How to Use Comparator?
The compare() method, shown here, compares two elements for order − ... obj1 and obj2 are the objects to be compared. This method returns zero if the objects are equal. It returns a positive value if obj1 is greater than obj2.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-comparator-interface
Java Comparator Interface - GeeksforGeeks
The method uses the compare() function of the given Comparator to decide the order of elements. The compare() method compares two elements and returns: -1 -> if the first element should come before the second ... import java.util.*; // Define ...
Published   November 25, 2025
🌐
Educative
educative.io › answers › what-is-objectscompare-in-java
What is Objects.compare() in Java?
If both the objects passed point to null reference, then the method returns zero. To use the compare() method, you must import the following module. ... The syntax of the compare() method is as follows. public static <T> int compare(T a, T b, Comparator<? super T> c)
🌐
Javatpoint
javatpoint.com › how-to-compare-two-objects-in-java
How to Compare Two Objects in Java - Javatpoint
How to Compare Two Objects in Java - How to Compare Two Objects in Java with java tutorial, features, history, variables, object, class, programs, operators, for-loop, oops concept, inheritance, array, string, map, math, methods, examples etc.
🌐
Medium
medium.com › @QA-initi › how-to-assert-that-the-two-objects-are-equal-in-java-using-comparator-c3a8a057902
How to assert that the two objects are equal in Java using comparator | by QA-init | Medium
January 19, 2023 - In this example, we will consider comparison between two Employees represented in form of Employee class and verify that they are equal. In order to check this we will create a custom comparator that compares the objects based on their attributes, for example id, name, salary, and address.
🌐
Lean Tactic Reference
course.ccs.neu.edu › cs5004 › lectureequality_and_comparison.html
Lecture 6: Equality and comparison
July 15, 2023 - Java facilitates this through the Comparator<T> interface. If two objects of class X must be compared, we can write a comparator as public class XComparator implements Comparator<X>. This object must now contain a method public int compare(X a, X b) that compares two objects of type X.
🌐
Blogger
javarevisited.blogspot.com › 2011 › 06 › comparator-and-comparable-in-java.html
Javarevisited: How to use Comparator and Comparable in Java? With example
While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. 3) If you see the logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified.
🌐
Java67
java67.com › 2019 › 06 › top-5-sorting-examples-of-comparator-and-comparable-in-java.html
6 Advanced Comparator and Comparable Examples in Java 8 | Java67
You can use thenComparing() method to chain multiple Comparators in Java 8 to compare objects by multiple fields like comparing a list of a person by name and by age or comparing a list of books by author and price as shown in the following example:
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-objects-by-multiple-fields-in-java
How to Compare Objects by Multiple Fields in Java? - GeeksforGeeks
July 23, 2025 - You want to compare objects of this class first by field1, then by field2, and finally by field3. Let's consider a scenario where you have a `Person` class with fields `name`, `age`, and `city`. We want to compare `Person` objects first by name, ...