File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes alphabetize a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "alphabetize a string" Watch "alphabetize a string" New topic

alphabetize a string

Kathy Talley

Joined: Mar 08, 2004
Posts: 3
Here is a start on the code for an application with a method that will alphabetize a string using the following signature:
public static String sort(String s)
I gave it a go, but I am so confused at this point I'm not sure what to do. The charArray string is not being recognized? Any help would be appreciated. I get really confused with methods, although I have been told over and over how to pass in data, etc. The name of the data is always different passing and returning...

[ April 20, 2004: Message edited by: Kathy Talley ]
[ April 20, 2004: Message edited by: Kathy Talley ]
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
The body of this method could be one line, but to break that one line down into several steps:
  • Convert the parameter string (let's call it 's') to a char array. There is a method in class String for this.
  • Use the utility sorting class Arrays to sort the char array.
  • Build and return a new String, using the constructor that takes a char array.

  • If you hunt around in the Javadoc you should be able to find the methods for these steps.
    On a side note, this sounds a little like an assignment question. If it is, it's worth confirming that you can make use of the full Java libraries. The second point in the list above is a whole lot easier than implementing your own sorting algorithm
    Dirk Schreckmann

    Joined: Dec 10, 2001
    Posts: 7023
    Note that the documentation for all of the classes in the Java 2 Standard Edition API is available at
    As suggested, you might find taking a look at the String class documentation for information on what methods are available to be useful.

    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    So what do you have so far. Please post some code and ask specific questions about what you are stuck on. We will be more than happy to help you there.

    Java API Documentation
    The Java Tutorial
    Tom Blough
    Ranch Hand

    Joined: Jul 31, 2003
    Posts: 263
    I just wanted to make sure you did not get the wrong impression from the replys to your post. Everyone here at the Ranch would love to HELP you. Most of the ranch hands who have been around a while, really want to help you learn how. Therefore, for what seems like a homework related question, we usually try to guide you to finding out the answer for yourself in lieu of posting a coded solution. Please do not mis-intrepret the lack of code as unhelpfulness.
    That said, Tim has sent you down the right track. Since you need to deal with each character in a string individually, it seems like the first thing you will need to do is break the string into individual characters. Since what you have to start with is a string, look at the API for the string class and see what is available there to get you individual characters.
    Tim has already posted that what you want is there. When you find it and read about it, you will see that it returns an array of chars. Great! Now you have individual characters stored in a simple group - an array. So, the next thing you should do it look in the API at the classes that deal with Arrays, and see what is available to help you sort them. Once you get your characters sorted, you need to put them back together into a string.
    To do that as, Tim already pointed out, look at the different ways you can make a String object and you'll find the answer. When dealing with objects, think about what you have (i.e. an Array or String), what you want (i.e. Strings, arrays, or chars) and look in the API at those kinds of classes. Usually, the API for what you have or what you want, will have the method you need to do the conversion.
    Now, Tim also mentioned that if this is an assignment, you may not be allowed to use the sorting methods in the Array utility classes. If so, you will need to write your own sorting method based on some common properties of chars. Chars are encoded using Unicode, but for our purposes we can look at the subset of Unicode called ASCII.
    In the ASCII encoding scheme, each character has a numeric value associated with it (in reality, each numeric value has a character associated with it). If you look at an ASCII chart such as this one, you'll see that the upper case letters are all in order and have smaller numbers associated with them than the lower case letters.
    In Java, char data can be treated as integers with the integer value being the number associated with the letter in the table. So, 'A' is less than 'Z' because 65 < 90. Similarily, 'A' is less than 'z' because 65 < 122.
    That should give you a start. As Layne wrote, when you get some code together, post it and well help some more!
    Tom Blough

    Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
    Tim West
    Ranch Hand

    Joined: Mar 15, 2004
    Posts: 539
    Ahh, hope I didn't sound unfriendly...I do like to encourage investigation, you learn heaps more that way! We are all happy to help though, as Tom says.
    Good luck with it

    [ April 14, 2004: Message edited by: Tim West ]
    I agree. Here's the link:
    subject: alphabetize a string
    It's not a secret anymore!