Guava is more elegant:
return ComparisonChain.start()
.compare(d1.weight, d2.weight)
.compare(d1.age, d2.age)
.compare(d1.name, d2.name)
.result();
Apache commons-lang has a similar construct, CompareToBuilder
.
More Related Contents:
- What is the internal sorting technique used in comparator and comparable interfaces and why? [duplicate]
- Sort ArrayList of custom Objects by property
- How to use Comparator in Java to sort
- When should a class be Comparable and/or Comparator?
- Java : Comparable vs Comparator [duplicate]
- When to use Comparable and Comparator
- Why does PriorityQueue.toString return the wrong element order? [duplicate]
- Very confused by Java 8 Comparator type inference
- Android-java- How to sort a list of objects by a certain value within the object
- Reverse a comparator in Java 8
- comparing and thenComparing gives compile error
- Why doesn’t java.lang.Number implement Comparable? [duplicate]
- How do I make 2 comparable methods in only one class?
- Comparable and Comparator contract with regards to null
- What does comparison being consistent with equals mean ? What can possibly happen if my class doesn’t follow this principle?
- Using Comparable for multiple dynamic fields of VO in java
- Java – Sort Strings like Windows Explorer
- Java Sorting: sort an array of objects by property, object not allowed to use Comparable
- Create a SortedMap in Java with a custom Comparator
- Write a function that will sort a string array using Java
- How do I use Comparator to define a custom sort order?
- Sorting 2D array of strings in Java
- Why does Collections.sort use Mergesort but Arrays.sort does not?
- Java Sorting based on Enum constants
- Sort a List of objects by multiple fields [duplicate]
- How to check if array is already sorted
- How can I sort a JSONArray in JAVA [duplicate]
- JTable + Sorting specific field
- ‘MergeSort Algorithm’ – What’s the better implementation in JAVA? [closed]
- java: Arrays.sort() with lambda expression