Reputation: 47
So I am a beginner in Java and was solving from a book where the question was :
Write a program that sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order. Display the sorted list of words. You could use a simple sorting method called the bubble sort. To sort an array into ascending order the process is as follows: a. Starting with the first element in the array, compare successive elements (0 and 1, 1 and 2, 2 and 3, and so on). b. If the first element of any pair is greater than the second, interchange the two elements. c. Repeat the process for the whole array until no interchanges are necessary. The array elements are now in ascending order.
To which my solution was:
public class bubbleSort {
public static void main(String[] args) {
String Homer = "He was the son of Epikaste and Telemachus. " +
"He was said to be a court singer ";
String swap;
Homer = Homer.replace(',', ' ');
Homer = Homer.replace('.', ' ');
Homer = Homer.replace(" ", " ");
String[] words = Homer.split(" ");
for(String val:words){
System.out.println(val);
}
System.out.println(" ---- SORTED -------");
boolean exchange = true;
while (exchange) {
exchange = false;
for (int i = 0; i < (words.length - 1); ++i) {
if (words[i].charAt(0) > words[i + 1].charAt(0)) {
swap = words[i];
words[i] = words[i + 1];
words[i + 1] = swap;
exchange = true;
}
}
}
for(String val:words){
System.out.println(val);
}
}
}
However the sorted output wasn't as intended !
He was the son of Epikaste and Telemachus He was said to be a court singer ---- SORETED ------- Epikaste He He Telemachus and a be court of son said singer the to was was
Where have I made a mistake? Thanks !
Upvotes: 0
Views: 445
Reputation: 15145
The simple way would be to convert all words to upper or lower case.
However, the correct way to compare words of a language in Java is to use the Collator
Collator myCollator = Collator.getInstance(); // optional: pass your locale
if( myCollator.compare("abc", "ABC") < 0 )
System.out.println("abc is less than ABC");
else
System.out.println("abc is greater than or equal to ABC");
This makes sure that words with special characters like 'è' or 'ä' are sorted correctly. While this makes no difference for your example, if you are learning Java, learn it right from the start.
In your example, create a collator instance at the beginning of the method and replace
if (words[i].charAt(0) > words[i + 1].charAt(0))
with
if (myCollator.compare(words[i], words[i+1]) > 0)
Upvotes: 2
Reputation: 2146
I guess what you don't like in the result is that it sorted uppercase (capitalized) words before lowercase words. This is not surprising, because uppercase characters A-Z use the code points 65 through 90, whereas lowercase chars a-z have code points 97 through 122. So uppercase chars are always "smaller" than lowercase chars in a comparision.
Solution: Convert all words to lowercase for the comparison. Use String.toLowerCase()
for that.
For example, instead of your original code
for (int i = 0; i < (words.length - 1); ++i) {
if (words[i].charAt(0) > words[i + 1].charAt(0)) {
do it more like
for (int i = 0; i < (words.length - 1); ++i) {
String w = words[i].toLowerCase();
String w1 = words[i+1].toLowerCase();
if (w.charAt(0) > w1.charAt(0)) {
Upvotes: 0
Reputation: 779
It is sorted Capital letters are less than lower case letters in ascii to get the correct sorting change all the capitals to lower case or vise versa while sorting
Upvotes: 2