954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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.

eyndyel
Newbie Poster
1 post since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

> 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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
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?

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

import java.util.*;

class StringSort
{

public static void main(String args[])
{

String str1="Hi how are you";
String []str;

str=str1.split(" ");

String []str2=new String[str.length];

for(int i=0;ijava StringSort
ARE
HI
HOW
YOU
*/

heenas
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Awesome! Eyndyel has been waiting TWO YEARS for someont to post that code. Thanks!

Seriously - what was the point of posting this, aside from showing off that you know how to sort Strings? (Hint: if that's what you've got to brag about, you might want to keep your head down and learn some new tricks.)

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You