Java: byte[] Array sorting with example

In this article, we will discuss how to sort byte[] array with example

Byte:

  • Size is 1 byte
  • Its range is -128 to 127

Arrays class has various sort methods for sorting different primitive data-types

 

Sorting byte[] array:

To sort byte[] array, we have 2 variant of sort methods from Arrays class,

Method Signature:

public static void sort(byte[] a);

public static void sort(byte[] a, int fromIndex, int toIndex);

 

Byte[] Array methods for Sorting:

Sort method

Description

sort(byte[]); sorts complete byte[] array
sort(b[], sIndex, eIndex); sorts partial byte[] array as per limits start-index & end-index specified in the method arguments

 

Let us move forward to discuss both methods for sorting byte[] array

Method 1: complete sorting of byte[] array

  • Here, complete array will be sorted

Method Signature:

public static void sort(byte[] a);

 

SortingCompleteByteArray.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingCompleteByteArray {

	public static void main(String[] args) {

		// sample byte[] array
		byte[] bArray = {14, 1, 29, 7, 13, 23, 17};

		// before sorting
		System.out.println("Before sorting : ");
		for(byte b : bArray) {
			System.out.print(b + " ");
		}

		// sorting full byte[] array
		Arrays.sort(bArray);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(byte b : bArray) {
			System.out.print(b + " ");
		}
	}
}

Output

Before sorting : 
14 1 29 7 13 23 17 

After sorting : 
1 7 13 14 17 23 29

 

Method 2: partial sorting of byte[] array

  • This is the another variant to sort array where we can specify start & end limits within byte[] array

Method Signature:

public static void sort(byte[] a, int fromIndex, int toIndex);

 

SortingPartialByteArray.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingPartialByteArray {

	public static void main(String[] args) {

		// sample byte[] array
		byte[] bArray = {14, 1, 29, 7, 13, 23, 17};

		// before sorting
		System.out.println("Before sorting : ");
		for(byte b : bArray) {
			System.out.print(b + " ");
		}

		// partial sorting of byte[] array
		Arrays.sort(bArray, 1, 6);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(byte b : bArray) {
			System.out.print(b + " ");
		}
	}
}

Output

Before sorting : 
14 1 29 7 13 23 17 

After sorting : 
14 1 7 13 23 29 17

 

Explanation:

  • Here, there are 7 byte elements in byte[] array
  • But, we have sorted byte[] array starting from index-1 till index-5 leaving 1st & last element
  • Therefore, 1st and last element remains as it is after sorting and only middle elements are sorted

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Byte.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

 

Happy Coding !!
Happy Learning !!