Java Reference
In-Depth Information
The function is pretty much the same as the previous ones except for the declaration of list and the use of
compareToIgnoreCase to compare two strings. If case matters, you can use compareTo .
We test insertionSort3 with Program P1.3.
Program P1.3
import java.util.*;
public class SortStrings {
final static int MaxNames = 8;
public static void main(String[] args) {
String name[] = {"Graham, Ariel", "Perrott, Chloe",
"Charles, Kandice", "Seecharan, Anella", "Reyes, Aaliyah",
"Graham, Ashleigh", "Reyes, Ayanna", "Greaves, Sherrelle" };
insertionSort3(name, 0, MaxNames - 1);
System.out.printf("\nThe sorted names are\n\n");
for (int h = 0; h < MaxNames; h++)
System.out.printf("%s\n", name[h]);
} //end main
// insertionSort3 goes here
} //end class SortStrings
When run, Program P1.3 produced the following output:
The sorted names are
Charles, Kandice
Graham, Ariel
Graham, Ashleigh
Greaves, Sherrelle
Perrott, Chloe
Reyes, Aaliyah
Reyes, Ayanna
Seecharan, Anella
1.5 Sorting Parallel Arrays
It is quite common to have related information in different arrays. For example, suppose, in addition to name , we have
an integer array id such that id[h] is an identification number associated with name[h] , as shown in Figure 1-1 .
 
 
Search WWH ::




Custom Search