Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to sort elements of a StringBuilder?

763890Mar 5 2010 — edited Mar 8 2010
Is there a way of sorting elements of a StringBuilder?

I am wondering if there is a better(more efficient?) way to do the sorting for the StringBuilder.
It seems like the following can get it done:

NOTE: StringBuilder: sb , String: s
Solution A:
1.Sort the s using java.util.Arrays.sort(+s+.getBytes())
2.Append the s in 1. to a sb using append method

Solution B:
1.Append unsorted s to a sb using append method
2.Sort the sb in 1. using java.util.Arrays.sort(+sb+.toString().getBytes)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 5 2010
Added on Mar 5 2010
17 comments
14,931 views