We are sorting the ArrayList with the Collections.sort() method, in which we are taking the ArrayList object as the parameter. Inside the public class M圜lass, we are creating an ArrayList object of the Students class, and we are storing different student's features(roll, name, marks) inside the ArrayList. In the above code, we have a Students class which consists of multiple students roll, name, and marks. it provides the compareTo method that takes a parameter of the object of that class. Let’s sort the above ArrayList based on their name.Įxample of Sorting an ArrayList Based on their Name Using Comparable InterfaceĬomparable : A comparable is an interface, that is used to compare an object of the same class with an instance of that class. We can also sort it on the basis of name and roll. We have sorted the Students class on the basis of their marks. MarksComparator class we have override the compare() method of the Comparator interface. The Comparator interface is defined inside the MarksComparator class. We are sorting the ArrayList with the Collections.sort() method, in which we are taking the ArrayList object as the parameter and also the Comparator interface ( MarksComparator). Inside the public class M圜lass, we are creating an ArrayList object of the Students class, and we are storing different student features(roll, name, marks) inside the ArrayList. Below is the syntax of how we can use the comparator method inside the collections.sort() method after rewriting the compare() method. We need to override the compare() method provided by the comparator interface to sort an ArrayList using Comparator. Without affecting the original User-defined class in any way, we can implement the comparator. With the help of a comparator, we can sort an ArrayList on the basis of multiple variables. In java, the comparator is defined inside the java.util package. The comparator can be also used for sorting a user-defined object in an ArrayList. Throughout the article, the comparator term is mentioned many times, let us now understand what is comparator.Ĭomparator : Comparator is an interface that is used for rearranging the elements of an ArrayList in sorted order. Here the reverseOrder() method of the Comparator interface sort the elements of the ArrayList in reverse order that is, in descending order. Inside the method, we have passed the Comparator.reverseOrder(). This time we have used the sort() method of the Collections class in a different way, with the object of ArrayList calling the sort() method with the dot (.) notation. In the above code, we have created an ArrayList of type Integer and added some elements to it. Syntax of ArrayList Sort()īelow given is the syntax for the Collections.sort() Method in java to sort an ArrayList in ascending order. Now let us see the syntax of the Collections.sort() Method in java. Basically, it takes the elements of ArrayList as parameters and sorts them in ascending order, and returns an ArrayList that is sorted in ascending order, with respect to the original ordering of its elements. Using the sort() method of the collections class we can sort ArrayList in ascending order in java. Let's discuss these sorting methods in detail. Mainly the collections class provides two methods to sort an ArrayList in Java in ascending or descending order. An ArrayList can be sorted in two ways ascending or descending order. To arrange the data in a sorted manner we use different methods. Usually, the elements are stored in an unsorted manner. in which multiple elements or objects can be stored. In Java, there is a collection framework that provides interfaces like List, Set, Stack, Queue, etc., and also classes like LinkedList, ArrayList, etc. So here, in this article, we are going to learn how we can sort an ArrayList in java, to arrange the elements in an ArrayList in ascending or descending order. Note: An ArrayList in java can store the elements in an unsorted manner, not in ascending or descending order but just index-wise. So we can add as many elements as we want inside an ArrayList, but it should not exceed the memory limit the JVM has available. An ArrayList is a dynamic data structure that stores elements in it dynamically, that is it does not have any size limits. It usually inherits the properties of the AbstractList class. Before getting started with the topic of sorting ArrayList in java, let us get a short overview of what is an ArrayList in java.Īrraylist in java : An ArrayList** in java is a Collections framework class which exists inside the java.util package. In this article, we are going to learn about how to sort ArrayList in java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |