How to sort elements in Long Array in Java?
Declaration :
1 |
public static void sort(long[] a) |
Explanation :
Purpose | The java.util.Arrays.sort(long[]) method sorts the specified array of longs 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 long array long arrL[] = { 200, 400, 100, 500, 300 }; // Print the Unsorted Array List System.out.println("The Unsorted Long Array is:"); for (long num : arrL) { System.out.println("Array Element is " + num); } // Sort the array Arrays.sort(arrL); // Print the Sorted Array List System.out.println("The sorted long array is:"); for (long num : arrL) { System.out.println("Array Element is " + num); } } } |
Output of Program :
1 2 3 4 5 6 7 8 9 10 11 12 |
The Unsorted Long Array is: Array Element is 200 Array Element is 400 Array Element is 100 Array Element is 500 Array Element is 300 The sorted long array is: Array Element is 100 Array Element is 200 Array Element is 300 Array Element is 400 Array Element is 500 |