OnJava8-Examples/arrays/StringSorting.java

40 lines
1.4 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// arrays/StringSorting.java
2016-12-30 17:23:13 -08:00
// (c)2017 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.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Sorting an array of Strings
2015-06-15 17:47:35 -07:00
import java.util.*;
import onjava.*;
2016-01-25 18:05:55 -08:00
import static onjava.ArrayShow.*;
2015-06-15 17:47:35 -07:00
public class StringSorting {
public static void main(String[] args) {
2016-01-25 18:05:55 -08:00
String[] sa = new Rand.String().array(20);
show("Before sort", sa);
2015-06-15 17:47:35 -07:00
Arrays.sort(sa);
2016-01-25 18:05:55 -08:00
show("After sort", sa);
2015-06-15 17:47:35 -07:00
Arrays.sort(sa, Collections.reverseOrder());
2016-01-25 18:05:55 -08:00
show("Reverse sort", sa);
2015-06-15 17:47:35 -07:00
Arrays.sort(sa, String.CASE_INSENSITIVE_ORDER);
2016-01-25 18:05:55 -08:00
show("Case-insensitive sort", sa);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2016-07-22 14:45:35 -06:00
Before sort: [btpenpc, cuxszgv, gmeinne, eloztdv, ewcippc,
ygpoalk, ljlbynx, taprwxz, bhmupju, cjwzmmr, anmkkyh,
fcjpthl, skddcat, jbvlgwc, mvducuj, ydpulcq, zehpfmm,
zrxmclh, qgekgly, hyoubzl]
After sort: [anmkkyh, bhmupju, btpenpc, cjwzmmr, cuxszgv,
eloztdv, ewcippc, fcjpthl, gmeinne, hyoubzl, jbvlgwc,
ljlbynx, mvducuj, qgekgly, skddcat, taprwxz, ydpulcq,
ygpoalk, zehpfmm, zrxmclh]
Reverse sort: [zrxmclh, zehpfmm, ygpoalk, ydpulcq, taprwxz,
skddcat, qgekgly, mvducuj, ljlbynx, jbvlgwc, hyoubzl,
gmeinne, fcjpthl, ewcippc, eloztdv, cuxszgv, cjwzmmr,
btpenpc, bhmupju, anmkkyh]
Case-insensitive sort: [anmkkyh, bhmupju, btpenpc, cjwzmmr,
cuxszgv, eloztdv, ewcippc, fcjpthl, gmeinne, hyoubzl,
jbvlgwc, ljlbynx, mvducuj, qgekgly, skddcat, taprwxz,
ydpulcq, ygpoalk, zehpfmm, zrxmclh]
2015-09-07 11:44:36 -06:00
*/