Java – byte[] Arrays Sorting with example

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

1. Byte:

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

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

2. Sorting byte[] Arrays :

  • 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);

3. Sorting method for byte[] Arrays :

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

4. Example for byte[] Arrays Sorting :

  1. Complete/full Sorting of byte[] Arrays
  2. Partial Sorting of byte[] Arrays

4.1 Complete/Full Sorting of byte[] Arrays :

  • Here, complete arrays 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

4.2 Partial Sorting of byte[] Arrays :

  • This is the another variant to sort arrays 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 elements
  • 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:

Related Articles:

Happy Coding !!
Happy Learning !!

Java - char[] Arrays sorting with example
Java - How to Sort HashSet in 2 ways ?