- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort Byte Array in Java
A byte array can be sorted using the java.util.Arrays.sort() method with a single argument required i.e. the array to be sorted. A program that demonstrates this is given as follows −
Example
import java.util.Arrays; public class Demo { public static void main(String[] args) { byte[] arr = new byte[] { 4, 1, 9, 7, 5}; System.out.print("The original byte array is: "); for (byte i: arr) { System.out.print(i + " "); } Arrays.sort(arr); System.out.print("
The sorted byte array is: "); for (byte i: arr) { System.out.print(i + " "); } } }
Output
The original byte array is: 4 1 9 7 5 The sorted byte array is: 1 4 5 7 9
Now let us understand the above program.
First the byte array arr[] is defined and then displayed using a for loop. A code snippet which demonstrates this is as follows −
byte[] arr = new byte[] { 4, 1, 9, 7, 5}; System.out.print("The original byte array is: "); for (byte i: arr) { System.out.print(i + " "); }
The Arrays.sort() method is used to sort the byte array. Then the resultant sorted array is displayed using for loop. A code snippet which demonstrates this is as follows −
Arrays.sort(arr); System.out.print("
The sorted byte array is: "); for (byte i: arr) { System.out.print(i + " "); }
- Related Articles
- Filling byte array in Java
- Create BigInteger from byte array in Java
- Get byte array from BigInteger in Java
- How to concatenate byte array in java?
- Convert byte Array to Hex String in Java
- Convert Hex String to byte Array in Java
- How to print a byte array in Java?
- Java Program to fill elements in a byte array
- How to convert BLOB to Byte Array in java?
- How to convert Byte Array to Image in java?
- How to convert Image to Byte Array in java?
- How to convert InputStream to byte array in Java?
- Java Program to convert byte[] array to String
- Java Program to convert String to byte array
- Parsing and Formatting a Byte Array into Binary in Java

Advertisements