Sort array in java

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.

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:

Before Sorting
===============
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.

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:

Before Sorting
===============
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.

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:

Before Sorting
===============
[ 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.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.