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

Help with sorting strings alphabetically

Hi there.

I received an assignment to sort Strings alphabetically which a user inputs.

So far I've been able to produce the first word that would be top in the sort, so if someones inputs ("All Big Cats"), the first would be All.

Anyways that is where I am now stuck. I'm having trouble getting the other words to come after. Apparantly I'm supposed to have another method that uses the first method in order the produce the final result, using the return statement in the first method. I need help with the finalresult method.

What I have for the first method is this:

public static String sortline (String strword){
  String str = strword;
  String firstword="zzzzzzzzzzzzzzzzzzzzzz";
  int indexstart=0;
  
  for (int i=0; i <str.length();i++){
    //System.out.println(indexstart+"");
    if (Character.isLetter(str.charAt(i))){
      if(i!=0 && !Character.isLetter(str.charAt(i-1))){
        indexstart = i;
      }
    }else{
        if(i!=0 && Character.isLetter(str.charAt(i-1))){
          if ((firstword.compareToIgnoreCase(str.substring(indexstart,i)))>0){
            firstword = str.substring(indexstart,i);
           // System.out.println(firstword);
          }
        }
    }
  }
  if (Character.isLetter(str.charAt(str.length()-1))){
    if(str.substring(indexstart,str.length()).compareToIgnoreCase(firstword)<0){
      firstword = str.substring(indexstart,str.length());
    }
  }
  System.out.println("First word is:" +firstword);
  return firstword;
  }

Appreciate any help given.

Nitroxxerz
Newbie Poster
2 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

You should search through the forums a bit more :) .

http://www.daniweb.com/forums/thread133150.html

PsychicTide
Junior Poster
114 posts since Aug 2008
Reputation Points: 54
Solved Threads: 15
 

You should search through the forums a bit more :) .

http://www.daniweb.com/forums/thread133150.html

I have looked that up before, but the thing is that it deals with arrays which mine doesn't. The codes are written differently, so I don't really understand the other one that well.

Nitroxxerz
Newbie Poster
2 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
I have looked that up before, but the thing is that it deals with arrays which mine doesn't. The codes are written differently, so I don't really understand the other one that well.


Well, what exactly are you sorting? What's the input and what is the output supposed to be?

Is it splitting a sentence into words, then sorting the words? If so, "All Big Cats" is already sorted and "Cats All Big" would turn into "All Big Cats". What's the goal here?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Well, what exactly are you sorting? What's the input and what is the output supposed to be?

Is it splitting a sentence into words, then sorting the words? If so, "All Big Cats" is already sorted and "Cats All Big" would turn into "All Big Cats". What's the goal here?

VernonDozier is right. Your code doesn't make any sense. What exactly are you trying to accomplish and what are your requirements.

Also the String class has this method:
"compareTo"
It is used to compare 2 strings.
If it returns 0 means that the Strings are equal.
If it returns positive the first is "greater" than the second
If it returns negative the first is "lower" than the second.

So if sorting is your game, you can use this method to sort in the way you would sort numbers:

String a="abc";
String b = "def";

if (a.compareTo(b)>0) { // a > b
  System.out.println("a should go after b");
} else { // a <= b
  System.out.println("a should go before b");
}
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

I'm not sure about this, but I think using Collections.sort(yourArray) would also do the same thing javaAddict is suggesting. But it seems like your assignment is to write the sorting method yourself, so you'll need to specify what vernon mentioned.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
I'm not sure about this, but I think using Collections.sort(yourArray) would also do the same thing javaAddict is suggesting. But it seems like your assignment is to write the sorting method yourself, so you'll need to specify what vernon mentioned.

Actually we don't know what his assignment is, that is why I asked for the specifications. His code though seems that tries to compare 2 Strings, which is why I recommended that method.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Just add the strings in a TreeSet. All the Strings will be automatically sorted alphabetically.
You may check
SNIP
for more details on it. Though the code here is for numbers, it applies the same for Strings.

amitrail
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You