1. Overview
In this tutorial, we'll learn how to sort arrays in java with the comparator.
Comparator is used to write the custom sorting with the Collections or over Arrays.
If you are new arrays, read articles on Arrays.
At the end of the article, we have given the GitHub link where you can see all code at once and you can run the programs along with me.
It is easy to sort the arrays when the array is single-dimensional. Use Arrays.sort() method.
But if you have the user defined objects or two dimensional arrays then it is not possible to sort by using just the Arrays sort() method.
In this case, we need to use the Comparator interface to provide the custom logic for it.
First, we will start with the custom objects and then next show the two or more dimensional arrays.
You will see the different ways to create the comparator in jdk8, java 7 and before versions. All will produce the same output.