• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sandeeps srivastava
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic