How to Sort String Array in java


In this example we will show how to sort an array of String with Arrays.sort method in Java.

Source Code

package com.beginner.examples;

import java.util.Arrays;

public class SortStringArray {

	public static void main(String[] args) {
		
		//Create String Array.
		String[] stringNames = new String[]{ "Bob" ,"Alice", "Danny", "Jack","Chris"};
		
		/*
		 * To sort String array in Java, use Arrays.sort method.
		 */
		Arrays.sort(stringNames);
		
		for(int i=0; i < stringNames.length; i++){
			System.out.println(stringNames[i]);
		}
	}
}

Output:

Alice
Bob
Chris
Danny
Jack

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments