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
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
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448