Sorting Strings

Reply

Join Date: Oct 2008
Posts: 1
Reputation: eyndyel is an unknown quantity at this point 
Solved Threads: 0
eyndyel eyndyel is offline Offline
Newbie Poster

Sorting Strings

 
0
  #1
Oct 26th, 2008
I'm currently trying to make a mini dictionary. To do it i need to learn how to sort strings in alphabetical order. Can anyone tell me or give me a hint as to how am i going to sort the words in alphabetical order.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Sorting Strings

 
0
  #2
Oct 26th, 2008
The String method compareToIgnoreCase should do the trick. For example:
  1. int result = string1.compareToIgnoreCase(string2);
  2. // here result < 0 if string1 > string2
  3. // or result = 0 if string1 == string2
  4. // or result > 0 if string1 < string2

That should give you a start anyway. Have a go and see what you can come up with.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Sorting Strings

 
0
  #3
Oct 26th, 2008
> Can anyone tell me or give me a hint as to how am i going to sort the words
> in alphabetical order.

Look into the Collections#sort method which takes a Comparator instance to facilitate custom sorting.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,566
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Sorting Strings

 
0
  #4
Oct 26th, 2008
Originally Posted by darkagn View Post
The String method compareToIgnoreCase should do the trick. For example:
  1. int result = string1.compareToIgnoreCase(string2);
  2. // here result < 0 if string1 > string2
  3. // or result = 0 if string1 == string2
  4. // or result > 0 if string1 < string2

That should give you a start anyway. Have a go and see what you can come up with.
Are you sure about that? I thought it just added the value of the separate characters for each String, then compared those values. Which would not put them in alphabetical order. I'm not saying you're wrong, just curious.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Sorting Strings

 
0
  #5
Oct 27th, 2008
Originally Posted by BestJewSinceJC View Post
Are you sure about that? I thought it just added the value of the separate characters for each String, then compared those values. Which would not put them in alphabetical order. I'm not saying you're wrong, just curious.
No, the compareToIgnoreCase method's documentation in the API states that it compares the two strings "lexicographically", meaning that they are compared according to where they would be placed in a dictionary. The documentation also states that locales are ignored, but I'm not entirely sure what they mean by that, perhaps accented characters as seen in French or the umlaut in German are not considered?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC