Share on Google+Share on Google+

String sort() Method

In this program you will learn how to sort words in a String. The sorting will be done in ascending order.

String sort() Method


In this program you will learn how to sort words in a String. The sorting will be done in ascending order. We are going to use sort() method of String class in Java. The description of the code is given below for the usage of the method.

Description of the code:

Here, you will get to know about the sort() method through the following java program. In the program code given below, we have taken a String. First of all we will convert that string in array of characters. For this we have used toCharArray(); method. After that we have used sort(); method to sort that array of characters. 

The code of the program is given below:

import java.util.*;
import java.lang.String;

public class SortWords{
  public static void main(String args[]){
  String str = "This is my new string";
  char[] content = str.toCharArray();
  String sorted = new String(content);

Output of the program:

C:\unique>javc SortWords

C:\unique>java SortWords


Download this example.



Posted on: August 18, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: String sort() Method   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 7, 2011
string sorting in java

i dnt knw string sorting in java if u possible than give some example or logic.
edrizz fariento
October 6, 2011

its really hard!!!!!!!!!!
Mayssa Obaid
January 1, 2012
about String Methods in Java

Write a program that arranges three entered words in alphabetical order. Use the string functions to solve this program. thanks
February 23, 2012
case sensitive

how can i sort whit no case sensitiving?
August 20, 2012
not complete

need to sort words alphabetically like in dictionary and not sorting abc.....
July 2, 2014

java is good