944,005 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1600
  • Java RSS
Oct 26th, 2008
0

Sorting Strings

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eyndyel is offline Offline
1 posts
since Oct 2008
Oct 26th, 2008
0

Re: Sorting Strings

The String method compareToIgnoreCase should do the trick. For example:
java Syntax (Toggle Plain Text)
  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.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Oct 26th, 2008
0

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Oct 26th, 2008
0

Re: Sorting Strings

Click to Expand / Collapse  Quote originally posted by darkagn ...
The String method compareToIgnoreCase should do the trick. For example:
java Syntax (Toggle Plain Text)
  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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 27th, 2008
0

Re: Sorting Strings

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?
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Dec 18th, 2010
0
Re: Sorting Strings
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;i<str.length;i++)
{
str2[i]=str[i].toUpperCase();
}

Arrays.sort(str2);

for(int l=0;l<str2.length;l++)
{
System.out.println(str2[l]);
}
}
}
/*Output:
C:\Users\asd>java StringSort
ARE
HI
HOW
YOU
*/
Reputation Points: 10
Solved Threads: 0
Newbie Poster
heenas is offline Offline
1 posts
since Dec 2010
Dec 18th, 2010
0
Re: Sorting Strings
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.)
Reputation Points: 383
Solved Threads: 186
Posting Virtuoso
jon.kiparsky is offline Offline
1,837 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to programmatically separate JToolBar from JPanel?
Next Thread in Java Forum Timeline: SCJA/SCJP certifications





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC