DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Sorting Strings (http://www.daniweb.com/forums/thread153591.html)

eyndyel Oct 26th, 2008 9:14 am
Sorting Strings
 
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.

darkagn Oct 26th, 2008 9:57 am
Re: Sorting Strings
 
The String method compareToIgnoreCase should do the trick. For example:
int result = string1.compareToIgnoreCase(string2);
// here result < 0 if string1 > string2
// or result = 0 if string1 == string2
// or result > 0 if string1 < string2

That should give you a start anyway. Have a go and see what you can come up with.

~s.o.s~ Oct 26th, 2008 11:09 am
Re: Sorting Strings
 
> 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.

BestJewSinceJC Oct 26th, 2008 6:59 pm
Re: Sorting Strings
 
Quote:

Originally Posted by darkagn (Post 721764)
The String method compareToIgnoreCase should do the trick. For example:
int result = string1.compareToIgnoreCase(string2);
// here result < 0 if string1 > string2
// or result = 0 if string1 == string2
// 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.

darkagn Oct 27th, 2008 12:25 am
Re: Sorting Strings
 
Quote:

Originally Posted by BestJewSinceJC (Post 721996)
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?


All times are GMT -4. The time now is 4:29 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC