Home Java String-examples String sort() Method



String sort() Method
Posted on: August 18, 2007 at 12:00 AM
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.io.*;
import java.lang.String;

public class SortWords{
  public static void main(String args[]){
  String str = "This is my new string";
  char[] content = str.toCharArray();
  java.util.Arrays.sort(content);
  String sorted = new String(content);
  System.out.println(content);
  }
}

Output of the program:

C:\unique>javc SortWords

C:\unique>java SortWords
Teghiiimnnrssstwy

C:\unique>

Download this example.

Related Tags for String sort() Method:
javacstringarrayconvertsortmethodgetcharcharactercharacterscteprogramtorameilfirstinnomouttrjthroughallmehrprossoatrackirhallivrayfollowaractcodcodestrrtwingvassriringthavbelostabhatprodeonogrolo


More Tutorials from this section

Ask Questions?    Discuss: String sort() Method   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.