- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sorting Elements of Arrays and Wrapper Classes that Already Implements Comparable in Java
Java provides a variety of sorting algorithms and methods that can help us to sort arrays, lists or any collections. The comparable interface is an additional way that is useful when we want to sort custom objects by their natural ordering. For example, It sorts strings in dictionary order and numerics in numerical order. This interface is available in ‘java.lang’ package.
In this article, we will create an array and arraylist and then, try to sort them to show that arrays and wrapper classes already implements Comparable Interface.
Sorting elements of Arrays and Wrapper class
We will use the following methods to sort elements of the collections and arrays −
Collections.sort() method
The class ‘Collections’ of the Collection Interface provides a static method named ‘Collections.sort()’ that can sort elements of specified collections like ArrayList or LinkedList. It is available in ‘java.util’ package.
Syntax
Collections.sort( nameOfcollection );
Arrays.sort() method
It is a static method of class Arrays that takes an argument and sort its elements accordingly. This method can sort arrays of numeric datatypes like integers or doubles and even arrays of characters, string too.
Syntax
Arrays.sort( nameOfarray);
Algorithm
Step 1 − We will begin with importing ‘java.util’ package so that we can use the methods discussed above.
Step 2 − Now create a method ‘araySort’. Inside this method, declare and initialize an array named ‘aray’ of type integer. Moving further, we will use the in-built method ‘Arrays.sort()’ to sort the elements of given array in ascending order. Then, using for each loop we will print the newly sorted array.
Step 3 − Create another user-defined method named ‘araylistSort()’. Inside this method, we define an arraylist of wrapper class Integer and using ‘add()’ method, we will add elements to it. Now use in-built method ‘Collections.sort()’ to sort the elements of given arraylist in ascending order. Then, use ‘get()’ method inside for loop to print elements of arraylist in sorted order.
Step 4 − At last, inside the main() method, we will call both methods to perform their individual operations.
Example
import java.util.*; public class Comp1 { public static void araySort() { int aray[] = {9, 3, 56, 0, -2, -6, 2, 1, 80}; System.out.print("The given unsorted array: "); // to print original unsorted array for (int print : aray) { System.out.print(print + " "); } Arrays.sort(aray); // to sort the given array System.out.println(); System.out.print("The newly sorted array: "); // to print newly sorted array for (int print : aray) { System.out.print(print + " "); } System.out.println(); } public static void araylistSort() { // Creating arraylist of Wrapper class Integer ArrayList<Integer> araylist = new ArrayList<Integer>(); // Adding elements in arraylist araylist.add(8); araylist.add(5); araylist.add(2); araylist.add(9); araylist.add(4); araylist.add(7); System.out.println("Elements of the list : "); // loop to iterate through elements for(int i = 0; i < araylist.size(); i++ ) { // to print the elements in the list System.out.print(araylist.get(i) + " "); } Collections.sort(araylist); // to sort the collection System.out.println(); System.out.println("Elements of the newly sorted list : "); for(int i = 0; i < araylist.size(); i++ ) { // to print the elements of newly sorted list System.out.print(araylist.get(i) + " "); } } public static void main(String args[]) { // method call araySort(); araylistSort(); } }
Output
The given unsorted array: 9 3 56 0 -2 -6 2 1 80 The newly sorted array: -6 -2 0 1 2 3 9 56 80 Elements of the list : 8 5 2 9 4 7 Elements of the newly sorted list : 2 4 5 7 8 9
The methods we have created are static so we don’t need to create any object to call them.
Conclusion
You must have noticed that we have not implemented the Comparable Interface explicitly but we are able to sort the elements of arrays and wrapper classes using ‘sort()’. The reason is that they implicitly implement the Comparable Interface.
In this article, we have understood how these two inbuilt methods ‘Arrays.sort()’ and ‘Collections.sort()’ are useful when it comes to sorting.