This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A Function for Lexicographical Sort on Input String  RSS feed

 
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

It's been a long time since I've been here (following my lack of using Java). The new site looks fantastic! Anyways...

I was sifting through Java documentation for a function that would take a String as a parameter, and return another String. The string to be returned should be the argument sorted into alphabetical order. I'm using this to check if two words contain the same characters, and are thus anagrams (as an aside, I noticed BitSets in the docs, which almost makes me think I could compare relative sets, two words, of characters as numbers. If the sets are equivalent, then they contain the same letters).

example of what I would like to see:






Does such a function exist already? It's been a while and at first attempt to this problem I have the not most efficient idea of breaking down a string into a character array, then looping through this array and sorting the letters, using some comparable interface. Not very efficient, indeed.

Thanks guys, and I'm glad to be back!
T.
 
author
Marshal
Posts: 23438
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charley Grossman wrote:
Does such a function exist already? It's been a while and at first attempt to this problem I have the not most efficient idea of breaking down a string into a character array, then looping through this array and sorting the letters, using some comparable interface. Not very efficient, indeed.



The String class has a method that returns the underlying char array. And the Arrays class has a method that can sort that array.

Henry
 
Charley Grossman
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry! I didn't realize the Arrays class has it's own alphabetic sorting function. I see it now
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!