JAVA COLLECTIONS SORT CODE
So let's get on with the good code where we make use of our Comparator. The resulting ArrayList would look like this:Īs you can see, that's not what we're looking for we want all the vowels to appear first. So you see here that we're populating an ArrayList with our “test” sentence and then we invoke the Collections.sort() method. TheArrayList.add("R") theArrayList.add("A") theArrayList.add("M") theArrayList.add("S") TheArrayList.add("P") theArrayList.add("R") theArrayList.add("O") theArrayList.add("G") TheArrayList.add("J") theArrayList.add("A") theArrayList.add("V") theArrayList.add("A") theArrayList.add(" ") TheArrayList.add("L") theArrayList.add("O") theArrayList.add("V") theArrayList.add("E") theArrayList.add(" ") TheArrayList.add("I") theArrayList.add(" ") construct the ArrayList with our letters
JAVA COLLECTIONS SORT HOW TO
So let's have a look at the code and how this can be made possible, here's the example of how to do a regular old sort of an ArrayList: public class SortingExample Well, since we are trying to sort an ArrayList of Strings then we will need to use the Comparator. Don't read anything else until you have come up with a guess in your mind. Okay, so given the information above, and knowing that we want to sort an ArrayList do you know which one of the interfaces you would use?
With this ArrayList, let's say you're given the requirements to sort all of the letters in a unique way. Let's assume you're presented with the following ArrayList: So let's start off with a simple example so that you have a clear understanding of what I'm talking about. That's a great question, and I'll answer it soon. You might ask “Why are there two different interfaces used for sorting?”. So how can this be done? And moreover, how can this be done in a way that's fully customizable? Comparator/ Comparable InterfacesĮnter the Comparator and Comparable interfaces… these are what we use to accomplish the sorting of Collections and Arrays in a customizable way. So clearly it should be used when you have a array available with you and you want to sort it.Have you run into a situation where you had a Collection of Objects (or an Array), and you needed to have them ordered in a certain way?Ĭhances are that if you've been programming with Java and running through the assignments available on this blog, then you have. So this should be used when you are trying to sort a list.Īrrays.sort is for arrays so the sorting is done directly on the array. Both methods have same algorithm the only difference is type of input to them.Ĭollections.sort() has a input as List so it does a translation of List to array and vice versa which is an additional step while sorting. Many developers are concerned about the performance difference between () & () methods. ArrayList and LinkedList extend List interface, so we can sort them using Collections.sort.Ĭollections.sort() has a time complexity of O(nlogn) as it run merge sort in background import java.util.* String names = Ĭollections.sort() is used to sort an object which extends List interface. Sort() method is best optimized, so if you use this method instead of writing your own, you'll get best results. TimSort algorithm makes use of the Insertion sort and the MergeSort algorithms. The time complexity for this method is O(nlogn) as it runs TimSort in background.
It can be integer array, float array, String array, Array of objects etc. It is used to sort the Array passed to it. Arrays.sort() is a method residing in Arrays class.