Java Sort Int Array Example
In programming languages, sorting plays a very significant role as it reduces the complexities. In Java, the term sorting means arranging the elements/values in a particular order i.e. ascending or descending. If we talk about array sorting, it can be achieved using some built-in methods, user-defined methods, using loops, etc. These sorting approaches are capable of sorting any type of array such as numeric, or alphabetic.
This article will elaborate on the below-listed array sorting procedures:
- How to Sort an Array Using sort() Method
- How to Sort an Array Using reverseOrder() Method
- How to Sort an Array Using Java for Loop
So, let's get started!
How to Sort an Array Using sort() Method
Java offers numerous procedures to sort an array and among them, the most simple and easy way is the use of built-in methods. A java predefined class named "Arrays" provides a static type method known as the "sort()" method which can be invoked/called directly with the class name. It sorts the array in ascending order and can take data of type int, char, float, byte, long, and double.
The best way to understand a concept is to experiment with it, so consider the below code snippet, which will assist us in understanding how to use the sort() method in java.
Example
In this example, we have a string-type array that consists of five elements. We will utilize the sort() method to arrange the array elements in ascending order:
import java.util.Arrays ;
public class SortingExample {
public static void main( String [ ] args) {
String [ ] arr = new String [ ] { "Joe", "Smith", "Bryn", "Wiliamson", "Alex" } ;
Arrays.sort (arr) ;
System.out.println ( "Sorted Array: " ) ;
for ( String arr1 : arr) {
System.out.println (arr1) ;
}
}
}
We passed the array to the "Arrays.sort()" method to sort the array in ascending order. Afterward, we utilized the for-each loop to iterate through each element of the array:
The snippet given above verified the working of the sort() method.
Now, what if we have to arrange the array elements in reverse order? Well in such a case, we have to utilize the reverseOrder() method of Java's predefined Collection class.
How to Sort an Array Using reversreOrder() Method
The reverOrder() is a static method which means it can be invoked directly with the class name.
Example
We will consider the same array as in the previous example, and this time we will utilize the reverseOrder() method to sort the array in reverse order:
public class SortingExample {
public static void main( String [ ] args) {
String [ ] arr = new String [ ] { "Joe", "Smith", "Bryn", "Wiliamson", "Alex" } ;
Arrays.sort (arr, Collections.reverseOrder ( ) ) ;
System.out.println ( "Sorted Array: " ) ;
for ( String arr1 : arr) {
System.out.println (arr1) ;
}
}
}
We passed the "array name" and "Collections.reverseOrder()" method to the Arrays.sort() method; consequently, we will get the following output:
The output authenticated the working of the reverseOrder() method.
How to Sort an Array Using Java for Loop
In java, we can sort the arrays using a for-loop. The example given below will let you understand how to sort an array using for loop:
public class SortingExample {
public static void main( String [ ] args) {
String [ ] arr = new String [ ] { "Joe", "Smith", "Bryn", "Wiliamson", "Alex" } ;
for ( int i = 0 ; i < arr.length ; i++ ) {
for ( int j = i + 1 ; j < arr.length ; j++ ) {
String tmp = null ;
if (arr[i].compareTo (arr[j] ) > 0 ) {
tmp = arr[i] ;
arr[i] = arr[j] ;
arr[j] = tmp;
}
}
System.out.println (arr[i] ) ;
}
}
}
In the above snippet, we performed the following functionalities:
- We utilized the nested for-loops to handle the two adjacent array elements.
- Afterward, we utilized the compareTo method to compare the (string-type) array elements with other array elements.
- Since we are working with strings, therefore, we utilized the compareTo() method. If we have to work with numeric values, then we can utilize the comparison operator.
- Within if-statement we utilized the temporary variable to swap array elements when needed.
The output for the above snippet will look like this:
This is how we can sort an array using for-loop in java.
Conclusion
To sort an array in java, various predefined methods, java loops, and user-defined methods can be used. A java array can be sorted either in ascending or in descending order. The Arrays.sort() method sorts the array in ascending order while Collections.reverseOrder() method can be used to sort the array in descending order. Moreover, swapping techniques can be utilized within the java loops to sort the array in ascending or descending order based on user choice. This write-up explained various aspects of sorting arrays in java.
stewartyounsholve.blogspot.com
Source: https://linuxhint.com/sort-array-java/
0 Response to "Java Sort Int Array Example"
Post a Comment