How to sort elements in Byte Array in Java?
Declaration :
1 |
public static void sort(byte[] a) |
Explanation :
Purpose | The java.util.Arrays.sort(byte[]) method sorts the specified array of bytes 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 byte array byte arr[] = { 10, 5, 15, 20 }; // Print the Unsorted Array List System.out.println("The unsorted byte array is:"); for (byte num : arr) { System.out.println("Array Element is " + num); } // sorting array Arrays.sort(arr); // let us print all the elements available in list System.out.println("The sorted byte array is:"); for (byte num : arr) { System.out.println("Array Element is " + num); } } } |
Output of Program :
1 2 3 4 5 6 7 8 9 10 |
The unsorted byte array is: Array Element is 10 Array Element is 5 Array Element is 15 Array Element is 20 The sorted byte array is: Array Element is 5 Array Element is 10 Array Element is 15 Array Element is 20 |