Sort elements in Short Array in Java
Declaration :
1 |
public static void sort(short[] a) |
Explanation :
Purpose | The java.util.Arrays.sort(short[]) method sorts the specified array of shorts into ascending numerical order. |
Parameters | a ===> This is the array to be sorted. |
Return Value | This method does not return any value. |
Exception | NA |
Java Program : Example
Below example will explain java.util.Arrays.sort() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.c4learn; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // Initialize unsorted short array short arrSh[] = { 20, 5, 15, 25, 10 }; // Print the unsorted short array list System.out.println("The Unsorted short array is : "); for (short num : arrSh) { System.out.println("Array element is " + num); } // Sort the array Arrays.sort(arrSh); // Print the Sorted Short Array list System.out.println("The sorted short array is:"); for (short num : arrSh) { System.out.println("Array element is " + num); } } } |
Output of Program :
1 2 3 4 5 6 7 8 9 10 11 12 |
The Unsorted short array is : Array element is 20 Array element is 5 Array element is 15 Array element is 25 Array element is 10 The sorted short array is: Array element is 5 Array element is 10 Array element is 15 Array element is 20 Array element is 25 |