Collections.sort
Completed
Read n words, sort them alphabetically with Collections.sort, then print them, then print them in reverse order with Collections.reverse.
Input
n, then n words.
Output
Two lines.
Example
Input
4 pear apple fig banana
Output
[apple, banana, fig, pear] [pear, fig, banana, apple]
Editor
Java
Hints
Collections holds static helpers for lists: sort, reverse, max, min, shuffle...
Solution explanation
Strings sort by Unicode value, so upper-case letters come before lower-case ones.
Show solution
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<String> words = new ArrayList<String>();
for (int i = 0; i < n; i++) {
words.add(sc.next());
}
Collections.sort(words);
System.out.println(words);
Collections.reverse(words);
System.out.println(words);
}
}