About this question
How do you sort an array in Java? Learn the different ways to arrange elements in ascending or descending order using built-in methods like Arrays.sort() or custom sorting techniques in Java.
How do you sort an array in Java? Learn the different ways to arrange elements in ascending or descending order using built-in methods like Arrays.sort() or custom sorting techniques in Java.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Java Expert
Answered on May 11, 2025
Sorting an array in Java is a common task and can be accomplished easily using built-in methods or custom sorting techniques. Below are the different ways you can sort an array in Java:
1. Using Arrays.sort() for Ascending Order:
The simplest way to sort an array in Java is by using the Arrays.sort() method, which sorts the elements in ascending order by default.
import java.util.Arrays;
int[] numbers = {5, 3, 8, 1, 2};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); // Output: [1, 2, 3, 5, 8]This method works for arrays of primitive types (like int, char, etc.) as well as objects.
2. Sorting in Descending Order:
To sort in descending order, you can use a custom comparator if you are working with objects or reverse the array after sorting:
import java.util.Arrays;
import java.util.Collections;
Integer[] numbers = {5, 3, 8, 1, 2};
Arrays.sort(numbers, Collections.reverseOrder());
System.out.println(Arrays.toString(numbers)); // Output: [8, 5, 3, 2, 1]Note that for primitive types like int[], you can sort them in descending order by using a workaround (like sorting in ascending order and then reversing).
3. Using Custom Sorting (Comparator):
If you're working with objects or need a custom sorting order, you can define your own comparator:
import java.util.Arrays;
import java.util.Comparator;
String[] words = {"apple", "banana", "cherry"};
Arrays.sort(words, Comparator.reverseOrder());
System.out.println(Arrays.toString(words)); // Output: [cherry, banana, apple]Why Sort Arrays?
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Java interview questions, answered
Guides, tips and career advice on Java from JanBask experts.
Java Frequently Asked J2EE Interview Questions and Answers You Must Read
Top 45 frequently asked Java J2EE interview questions to ace your interview. These J2EE questions are useful for freshers and…
Java Top 100+ Angular Interview Questions and Answers
Are you preparing for your next Angular interview? We have compiled the list of the top 100+ Angular Interview Questions &…
Java Top 51+ Simple Java Project Ideas for Beginners In 2025
Enhance your Java skills with our top 51+ project ideas! From beginner-friendly exercises to advanced applications, fuel your…
Java How Long Does It Takes To Learn Java?
Confused about How long does it take to learn Java ? Well, No worries! This blog will guide you with valuable information and…