Arrays.sort() Java
sorting in java

Hi Guy’s, Java being the most popular programming language comes with an efficient & versatile sorting technique. In this Article let’s dive into a sorting method i.e. Arrays.sort(), This sorting technique enable the java developer with super power to sort an Array effortlessly and arranging the elements in ascending or descending order as per the requirement.

Arrays.sort() java

Arrays.sort() is robust method using which a developer can sort arrays of primitive data types or an objects.

Syntax

Arrays.sort(arrayList);

Sorting array of object in java

To sort an array of objects, we need a custom comparator or ensure that the array implement the comparable interface.

Arrays.sort(arrayList,Comparator);


Code Example to use Arrays.sort() method

1. Sorting an array of integers

import java.util.Arrays;

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

2. Sorting Array of strings

import java.util.Arrays;

public class ArraySortExample {
    public static void main(String[] args) {
        // Sorting an array of strings
        String[] stringArray = {"banana", "apple", "cherry", "date", "elderberry"};
        System.out.println("Original string array: " + Arrays.toString(stringArray));
        Arrays.sort(stringArray);
        System.out.println("Sorted string array: " + Arrays.toString(stringArray));
    }
}
Original string array: [banana, apple, cherry, date, elderberry]
Sorted string array: [apple, banana, cherry, date, elderberry]

3. Sorting Array of Object

In below sorting an array of object, I am sorting the object by name.

import java.util.Arrays;

public class ArraySortExample {
    public static void main(String[] args) {
        // Sorting an array of custom objects
        Person[] personArray = {new Person("John", 30), new Person("Alice", 25), new Person("Bob", 35)};
        System.out.println("Original Person array: " + Arrays.toString(personArray));

        Arrays.sort(personArray, (a, b) -> a.getName().compareTo(b.getName()));
        System.out.println("Sorted Person array by name: " + Arrays.toString(personArray));
    }

   // class 
    static class Person {
        private String name;
        private int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public String toString() {
            return "Person{name='" + name + "', age=" + age + "}";
        }
    }
}
Original Person array: [Person{name='John', age=30}, Person{name='Alice', age=25}, Person{name='Bob', age=35}]
Sorted Person array by name: [Person{name='Alice', age=25}, Person{name='Bob', age=35}, Person{name='John', age=30}]

The above code example is to demonstrates how to use ‘Arrays.sort()’ method for aorting datatype like integersm string and a class object in java.