How to alphabetically sort a String in Java with just three lines of code

Tremaine Eto
Mar 14 · 2 min read
Photo by Tracy Adams on Unsplash

If you need to alphabetically sort a String in Java, then you’ll want to read on and memorize this order of operations.

First of all, there is no native function in Java to just take a String and then sort it.

The steps

  1. Sort the array of characters
  2. Create a new String using the array of characters

Examples of sorted Strings

  1. “gggggc” → “cggggg”
  2. “za” → “az”

The code

So to reiterate, first we are defining a character array and then initializing it with our input string, str, that has the toCharArray() function applied to it. The toCharArray() function returns exactly an array of characters, so the type matches.

Then, we use the Arrays.sort() method which is a class method of Java.util.Arrays. This does the sorting work for us.

However, the sorted array is still in the form of an array of characters. In order to get back to a String, we then create a new String and then pass the array of characters into its class constructor (basically, we just provide it as the sole argument).

With this, we have an alphabetically sorted String, and we can return it.

This can come in handy when you want to sort a String, and while it’s not immediately obvious, it’s something you can memorize and quickly apply the next time you need it.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Tremaine Eto

Written by

Full-time software engineer since 2016. UCLA Computer Science B.S. with Atmospheric and Oceanic Sciences minor, class of ‘16.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Tremaine Eto

Written by

Full-time software engineer since 2016. UCLA Computer Science B.S. with Atmospheric and Oceanic Sciences minor, class of ‘16.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store