aspose file tools*
The moose likes Beginning Java and the fly likes How to sort String, for example: abced. to change to abcde Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to sort String, for example: abced. to change to abcde" Watch "How to sort String, for example: abced. to change to abcde" New topic
Author

How to sort String, for example: abced. to change to abcde

sandeeps srivastava
Greenhorn

Joined: Jul 25, 2008
Posts: 7
hi ,

i am beginners in java , i have proble to sorting a string for example how we sort abecd into abcde in java
please help
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Let us know how far have you gone with the problem.
Hint:You can use the charAt() method and any sorting mechanism you wish to implement.


SCJP, SCWCD.
|Asking Good Questions|
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
1 - create an array of characters from String X;
2 - sort the character array;
3 - create a new String from the sorted array;
4 - reassign X to the newly created String;
5 - have a beer... err... cup of coffee!

Have a look at the String- and Arrays API docs for steps 1, 2 and 3:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11308
    
  16

Is this a homework assignment? What have you tried? are you allowed to use all the tools in the api, or do you have to write your own sort?

I'd strongly recommend you read our FAQ to get the most out of the JavaRanch.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to sort String, for example: abced. to change to abcde