• Post Reply Bookmark Topic Watch Topic
  • New Topic

A Function for Lexicographical Sort on Input String  RSS feed

 
Charley Grossman
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!