Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Sort an Array in Alphabetical Order in Java



Sorting is the process of arranging elements in a specific order(ascending or descending order). Here, we will sort an array in alphabetical order means arranging the elements of the array based on lexicographical (dictionary) order.

To sort an array in alphabetical order is quite easy. Let's learn the following methods:

Sorting an array using Arrays.sort() Method

The Arrays.sort() method in Java sorts the elements of an array based on lexicographical (dictionary) order for strings. This method belongs to Java.util package.

Example

This program sorts an array of strings (alphabets) in alphabetical order using the Arrays.sort() method.

import java.util.Arrays;

public class SortArrayUsingArraysSort {
   public static void main(String[] args) {
      String[] alphabets = {"I", "N", "D", "I", "A"};

      Arrays.sort(alphabets);

      System.out.println("Sorted array in alphabetical order:");
      for (String alphabet : alphabets) {
         System.out.println(alphabet);
      }
   }
}

Output

Following is the output of the above program ?

Integer: 42
Sorted array in alphabetical order:
A
D
I
I
N

Sorting an array using compareTo() Method

The compareTo() method is part of the Comparable interface in Java and is used to define the alphabetical order. We can implement this method to compare strings lexicographically.

Example

This program sorts an array of company names (companies) in alphabetical order using the compareTo() method within a lambda expression.

import java.util.Arrays;

public class SortArrayUsingCompareTo {
   public static void main(String[] args) {
      String[] companies = {"Wipro", "Google", "Infosys", "Microsoft"};

      Arrays.sort(companies, (a, b) -> a.compareTo(b));

      System.out.println("Sorted array in alphabetical order:");
      for (String company : companies) {
         System.out.println(company);
      }
   }
}

Output

The above program produce the following result ?

Sorted array in alphabetical order:
Google
Infosys
Microsoft
Wipro

Sorting an array using reverseOrder() Method

The reverseOrder() method is a utility in Java's Collections class used to sort collections in reverse (descending) order.

Example

This program sorts an array of blood groups (bloodGroups) in reverse alphabetical order using Arrays.sort() with Collections.reverseOrder().

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

public class SortArrayUsingReverseOrder {
   public static void main(String[] args) {
      String[] bloodGroups = {"AB+","O+", "B-", "A-", "B+"};

      Arrays.sort(bloodGroups, Collections.reverseOrder());

      System.out.println("Sorted array in reverse alphabetical order:");
      for (String bloodGroup : bloodGroups) {
         System.out.println(bloodGroup);
      }
   }
}

Output

The above program produce the following result ?

Sorted array in reverse alphabetical order:
O+
B-
B+
AB+
A-
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2024-12-26T20:44:44+05:30

878 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements