OnJava8-Examples/arrays/StringSorting.java

40 lines
1.5 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// arrays/StringSorting.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
// Sorting an array of Strings.
import java.util.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
public class StringSorting {
public static void main(String[] args) {
String[] sa = Generated.array(new String[20],
2015-11-03 12:00:44 -08:00
new RandomSupplier.String(5));
2015-12-02 09:20:27 -08:00
System.out.println(
"Before sort: " + Arrays.toString(sa));
2015-06-15 17:47:35 -07:00
Arrays.sort(sa);
2015-12-02 09:20:27 -08:00
System.out.println(
"After sort: " + Arrays.toString(sa));
2015-06-15 17:47:35 -07:00
Arrays.sort(sa, Collections.reverseOrder());
2015-12-02 09:20:27 -08:00
System.out.println(
"Reverse sort: " + Arrays.toString(sa));
2015-06-15 17:47:35 -07:00
Arrays.sort(sa, String.CASE_INSENSITIVE_ORDER);
2015-12-02 09:20:27 -08:00
System.out.println(
"Case-insensitive sort: " + Arrays.toString(sa));
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
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]
2015-09-07 11:44:36 -06:00
*/