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:
1 2 3 | 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 :
- Complete/full Sorting of byte[] Arrays
- Partial Sorting of byte[] Arrays
4.1 Complete/Full Sorting of byte[] Arrays :
- Here, complete arrays will be sorted
Method Signature:
1 | public static void sort( byte [] a); |
SortingCompleteByteArray.java
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 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:
1 2 3 4 5 | 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:
1 | public static void sort( byte [] a, int fromIndex, int toIndex); |
SortingPartialByteArray.java
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 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:
1 2 3 4 5 | 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:
- 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
Related Articles:
- Byte Arrays sorting
- char Arrays sorting
- short Arrays sorting
- Integer Arrays sorting
- Float Arrays sorting
- Double Arrays sorting
- Long Arrays sorting
- String Arrays sorting
- Java – How to Sort Arrays in Ascending and Descending order ?
- Java – String Arrays sorting in ascending & descending order
- Java – Sorting after merging two String[] Arrays
- Java – Sorting Arrays using Comparable and Comparator interface
- Java – How to Sort String[] arrays by its length in Ascending and Descending order ?
Happy Coding !!
Happy Learning !!