-2

Suppose we're given a sentence. Then, how would we arrange the words alphabetically? For example,

Sentence: Big Bang Theory is better than Two and a Half Men.

Arranged Sentence: a and Bang better Big Half is Men than Theory Two.

I was thinking of applying a loop through the alphabets and comparing it with the sentence, but I keep getting confused. I can't think of anything. Help!

M A
  • 71,713
  • 13
  • 134
  • 174
user3192201
  • 27
  • 1
  • 5
  • Split the sentence into a `List` of words, based upon whitespace, and then just use `Collections.sort()` – Stewart Jan 25 '15 at 16:13

3 Answers3

1

Use a StringTokenizer to tokenize the sentence and fill the words in a list. A StringTokenizer object uses the characters " \t\n\r\f" (the space character, the tab character, the newline character, the carriage-return character, and the form-feed character) by default as delimiters to split the sentence into tokens. Then call Collections#sort(List, Comparator) with a case-insensitive comparator.

String sentence = "Big Bang Theory is better than Two and a Half Men";
StringTokenizer tokenizer = new StringTokenizer(sentence);
List<String> words = new ArrayList<String>();
while(tokenizer.hasMoreTokens()) {
    words.add(tokenizer.nextToken());
}

Collections.sort(words, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return o1.compareToIgnoreCase(o2);
    }
});

In the above code, a custom comparator is added to use String.compareToIgnoreCase to ignore case when comparing two strings. If you don't provide a custom comparator, you would end up with a list of words sorted as Bang, Big, Half, Men, Theory, Two, a, and, better, is, than.

Read the Javadocs of the relevant classes:

http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

Javadoc for Collections.sort:

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)

M A
  • 71,713
  • 13
  • 134
  • 174
1

You can split the sentence into array of words, then sort this array with a defined Comparator class:

String sentence = "Big Bang Theory is better than Two and a Half Men";
String [] arr = sentence.split(" ");
Arrays.sort(arr, (String o1, String o2) -> o1.compareToIgnoreCase(o2));
for (String arr1 : arr) {
    System.out.println(arr1);
}
0

You dont have to manually loop over to sort the array (It's already being done by the Arrays sort method). Instead you could use Arrays.sort method to sort each word in a sentence by splitting the sentence by space and by providing String's inbuilt comparator CASE_INSENSITIVE_ORDER which will say how exactly you want to sort the data (Comparing two strings lexicographically, ignoring case) by comparating one String with the other. For instance, you could do something like:

String line = "Big Bang Theory is better than Two and a Half Men";
    String[] strArray = line.split(" ");//split by space so we get sentence word by word
    Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
    StringBuilder stringBuilder = new StringBuilder(line.length());//recreate your sentence back.
    for (int i = 0; i < strArray.length - 1; i++) {
        stringBuilder.append(strArray[i]);
        stringBuilder.append(" ");
    }
    stringBuilder.append(strArray[strArray.length - 1]);
    System.out.println(stringBuilder.toString());
SMA
  • 36,381
  • 8
  • 49
  • 73
  • I have never used Comparator. I don't know what it is. Please give me a little more info on that. Secondly, I also don't know StringBuilder. And what is @Override? – user3192201 Jan 25 '15 at 16:34
  • comparator compares one string with the other to sort the data and `Arrays.sort` internally iterates over the array. StringBuilder will accumulate all the words together in single object and then we use toString to get string sentence which was sorted back by calling toString method. – SMA Jan 25 '15 at 16:41