For this lab exercise, you are to write a program that reads in a number of Strings from the user, stores them in an array, and prints them out twice, once sorted alphabetically from A-Z, and then sorted alphabetically from Z-A.
String apple = "apple";
String banana = "banana";
apple.compareTo(banana); // returns a negative number
banana.compareTo(apple); // returns a positive number
apple.compareTo(apple); // returns 0
Enter the number of strings: 5
Enter a string: pear
Enter a string: apple
Enter a string: pineapple
Enter a string: orange
Enter a string: banana
Sorted:
apple
banana
orange
pear
pineapple
Reverse sorted:
pineapple
pear
orange
banana
apple
Copyright © 2024 Ian Finlayson | Licensed under a Creative Commons BY-NC-SA 4.0 License.