Sort Characters

Sort characters of a string in ascending order.

BeginnerTopic: Module 4: String Programs
Back

Java Sort Characters Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = sc.nextLine();

        char[] arr = s.toCharArray();
        Arrays.sort(arr);
        System.out.println("Sorted: " + new String(arr));
        sc.close();
    }
}
Output
Enter a string: dcba
Sorted: abcd

Understanding Sort Characters

We convert to char array and use Arrays.sort to order characters.

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents