Table of Contents
In this post, we will see how to sort an array in java.
There are various ways to sort array in java. You can implement different sorting algorithms to sort an array.
You can use Arrays.sort method to sort array in java. There are various overloaded versions of Arrays.sort method. You can go through it over here.
Java Sort Array
Let’s see some example to sort an array in java.
Sort Array of numbers
Sorting an array of numbers is very easy. You just need to use Array.sort method to sort array of numbers.
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 org.arpit.java2blog; import java.util.Arrays; public class SortArrayInt { public static void main(String[] args) { int arr[]={7,33,22,11,20,5,2}; System.out.println("Before Sorting"); System.out.println("==============="); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+ " "); } Arrays.sort(arr); System.out.println(); System.out.println("==============="); System.out.println("After Sorting"); System.out.println("==============="); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } } } |
When you run above code, you will get below output:
===============
7 33 22 11 20 5 2
===============
After Sorting
===============
2 5 7 11 20 22 33
Sort Array of Strings
Sorting an array of String is also very easy. You just need to use Array.sort method to sort array of Strings.This will sort array of String in ascending order.
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 org.arpit.java2blog; import java.util.Arrays; public class SortArrayString { public static void main(String[] args) { String arr[]={"Martin","Andy","John","Mary"}; System.out.println("Before Sorting"); System.out.println("==============="); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+ " "); } Arrays.sort(arr); System.out.println(); System.out.println("==============="); System.out.println("After Sorting"); System.out.println("==============="); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } } } |
When you run above code, you will get below output:
===============
Martin Andy John Mary
===============
After Sorting
===============
Andy John Martin Mary
Sort array of custom objects
For Sorting array of custom objects, Custom object should implement comparable interface and then pass it to Array.sort method.You can also create comparator object and pass it to Array.sort method.
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package org.arpit.java2blog; import java.util.Arrays; public class Employee implements Comparable<Employee>{ String name; int age; Employee(String name,int age) { this.name=name; this.age=age; } public static void main(String[] args) { Employee e1=new Employee("Martin", 20); Employee e2=new Employee("Andy", 18); Employee e3=new Employee("John", 22); Employee e4=new Employee("Mary", 21); Employee[] empArray={e1,e2,e3,e4}; System.out.println("Before Sorting"); System.out.println("==============="); for (int i = 0; i < empArray.length; i++) { System.out.print(empArray[i]+ " "); } Arrays.sort(empArray); System.out.println(); System.out.println("==============="); System.out.println("After Sorting"); System.out.println("==============="); for (int i = 0; i < empArray.length; i++) { System.out.print(empArray[i]+" "); } } public String toString() { return "[ name="+name+" age="+age+" ]"; } @Override public int compareTo(Employee e) { return name.compareTo(e.name); } } |
When you run above code, you will get below output:
===============
[ name=Martin age=20 ] [ name=Andy age=18 ] [ name=John age=22 ] [ name=Mary age=21 ] ===============
After Sorting
===============
[ name=Andy age=18 ] [ name=John age=22 ] [ name=Martin age=20 ] [ name=Mary age=21 ]
That’s all about Sorting an array in java.