25 lines
1.3 KiB
Java
25 lines
1.3 KiB
Java
//: arrays/StringSorting.java
|
|
// Sorting an array of Strings.
|
|
import java.util.*;
|
|
import net.mindview.util.*;
|
|
import static net.mindview.util.Print.*;
|
|
|
|
public class StringSorting {
|
|
public static void main(String[] args) {
|
|
String[] sa = Generated.array(new String[20],
|
|
new RandomGenerator.String(5));
|
|
print("Before sort: " + Arrays.toString(sa));
|
|
Arrays.sort(sa);
|
|
print("After sort: " + Arrays.toString(sa));
|
|
Arrays.sort(sa, Collections.reverseOrder());
|
|
print("Reverse sort: " + Arrays.toString(sa));
|
|
Arrays.sort(sa, String.CASE_INSENSITIVE_ORDER);
|
|
print("Case-insensitive sort: " + Arrays.toString(sa));
|
|
}
|
|
} /* Output:
|
|
Before sort: [YNzbr, nyGcF, OWZnT, cQrGs, eGZMm, JMRoE, suEcU, OneOE, dLsmw, HLGEa, hKcxr, EqUCB, bkIna, Mesbt, WHkjU, rUkZP, gwsqP, zDyCy, RFJQA, HxxHv]
|
|
After sort: [EqUCB, HLGEa, HxxHv, JMRoE, Mesbt, OWZnT, OneOE, RFJQA, WHkjU, YNzbr, bkIna, cQrGs, dLsmw, eGZMm, gwsqP, hKcxr, nyGcF, rUkZP, suEcU, zDyCy]
|
|
Reverse sort: [zDyCy, suEcU, rUkZP, nyGcF, hKcxr, gwsqP, eGZMm, dLsmw, cQrGs, bkIna, YNzbr, WHkjU, RFJQA, OneOE, OWZnT, Mesbt, JMRoE, HxxHv, HLGEa, EqUCB]
|
|
Case-insensitive sort: [bkIna, cQrGs, dLsmw, eGZMm, EqUCB, gwsqP, hKcxr, HLGEa, HxxHv, JMRoE, Mesbt, nyGcF, OneOE, OWZnT, RFJQA, rUkZP, suEcU, WHkjU, YNzbr, zDyCy]
|
|
*///:~
|