Sort ArrayList of custom Objects by property
Quickly sort an ArrayList of CustomObjects using Java 8's Comparator.comparing:
For pre-Java 8, here is a tailor-made Comparator:
By default, the sort is ascending. For a descending sort, tag on .reversed() with Java 8, or swap o1 and o2 for an old school flip.
Unveiling Comparator
A Comparator's compare method yields an int. The returned value is positive if the first object is greater, zero if identical, and negative if smaller.
When Strings decide to become sensitive
We sometimes need case-insensitive sort especially with Strings. Here is the magic trick:
Mastering the art of juggling conditions
To chain multiple sorting conditions, here's your tool:
Watch your step
It's easy to trip over not returning the correct numbers from compare. It ruins your sorting experience. Always triple-check that your custom Comparator respects the contract: negative integer, zero, or a positive integer. You could call it the sorting community's secret handshake.
Streamlining with advanced sorting tech
The stream team
In Java 8 onwards, the Streams API accelerates the sorting process:
Welcoming null values with open arms
When your property may be null, Comparator.nullsLast or Comparator.nullsFirst become your best friends to dodge NullPointerException:
The complex date sorting bash
For complex sorting logic on dates or other complicated requirements, curate a specialized Comparator:
Full auto mode: Sorting sans Comparator (using Comparable)
If your CustomObject class implements Comparable, sorting is a leisurely stroll:
This demands CustomObject to have an internal definition of compareTo, living up to Comparable's contract—interfacing for comparing objects of the same class directly.
Navigating through common pitfalls
Unstable sorting
Ensure your sorting logic is as deterministic as the rise of the sun, particularly with complex data types and comparisons. A non-consistent Comparator is like a ticking time bomb.
Sneaky equals & compareTo
If your custom object enjoys overriding equals, it’s generally a good idea to override compareTo as well. Mismatches between equals and compareTo can lead to strange results, especially with sorted collections like TreeSet.
Don’t drown in lambda expressions
While lambda expressions are as concise as a tweet, overusing them, especially with complex logic, might hide the idea under the carpet. Strike a balance between conciseness and readability, preferring method references or separate Comparator classes where it fits.
Was this article helpful?
