Different Ways to Sort Array in Java

In this article, we will explore different ways to Sort Array in Java. Let’s get started.

Sort Array in Java

Different Ways to Sort Array in Java

Sort Array in Java using Arrays.sort() method

One common approach is to use the Arrays.sort() method for arrays of primitive types and objects. Here’s how to use it:

import java.util.Arrays;

public class ArraySortingExample {
    public static void main(String[] args) {
        // Sorting an array of integers
        int[] intArray = {5, 2, 9, 1, 5, 6};
        Arrays.sort(intArray);
        System.out.println("Sorted intArray: " + Arrays.toString(intArray));

        // Sorting an array of strings
        String[] stringArray = {"apple", "banana", "cherry", "date", "blueberry"};
        Arrays.sort(stringArray);
        System.out.println("Sorted stringArray: " + Arrays.toString(stringArray));

        // Sorting in descending order
        Integer[] descArray = {9, 4, 7, 2, 8, 1};
        Arrays.sort(descArray, (a, b) -> b - a); // Using a custom comparator
        System.out.println("Sorted in descending order: " + Arrays.toString(descArray));
    }
}

Sorting Arrays of Objects with Comparable or Comparator

You can sort arrays of custom objects by implementing the Comparable interface in your object class or by providing a custom Comparator.

import java.util.Arrays;
import java.util.Comparator;

class Person implements Comparable<Person> {
    private String name;
    private int age;

    // Constructor and getters

    @Override
    public int compareTo(Person other) {
        return this.name.compareTo(other.name);
    }
}

public class ArraySortingExample {
    public static void main(String[] args) {
        Person[] people = {/* Array of Person objects */};
        
        // Sorting using Comparable (by name)
        Arrays.sort(people);

        // Sorting using a custom Comparator (by age)
        Arrays.sort(people, Comparator.comparingInt(Person::getAge));
    }
}

Using Collections for Arrays of Objects

If you have an array of objects, you can convert it to a list, sort it using Collections.sort(), and then convert it back to an array.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class Person {/* ... */ }

public class ArraySortingExample {
    public static void main(String[] args) {
        Person[] people = {/* Array of Person objects */};
        List<Person> personList = Arrays.asList(people);
        
        // Sorting the list using Collections
        Collections.sort(personList);

        // Convert the sorted list back to an array
        Person[] sortedPeople = personList.toArray(new Person[0]);
    }
}

Sort Array in Java using Custom Sorting Algorithms

If you need to sort arrays using custom sorting algorithms (e.g., Bubble Sort, Select Sort etc), you can implement the algorithms.

Read More : Sorting Algorithms in Java: A Comprehensive Guide

Leave a Reply

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