Java array sorting: How to sort a Java String array

Java array FAQ: Can you share an example of how to sort a Java String array?

Sure. The following Java source code shows how to sort an array of strings. In short, you can easily sort a string array with the Arrays class sort method, shown in bold below:

import java.util.Arrays;

public class JavaSortStringArray
  public static void main(String[] args)
    // create a Java String array
    String[] fruits = {"banana", "orange", "kiwi", "apple"};
    // sort the array, using the sort method of the Arrays class
    // print the sorted results
    for (String fruit : fruits)

As you might imagine, the output from this array sorting example looks like this:



As mentioned, the only “trick” in this array sorting example is being aware of the Arrays class, and the sort method of that class.

I decided to write this tutorial after seeing someone write their own string array sorting algorithm. I know it’s hard sometimes to be aware of the complete Java API, so I hope that by sharing this array sorting example, this solution will be easier to find.

Java String array - What’s Related

As I wrap up my collection of “Java String array” tutorials, here are a few links to my other String array tutorials:

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.