/*
* 1, given a character array, sorted from small to large in dictionary order
* string[] arr = {"NBA", "abc", "CBA", "ZZ", "QQ", "haha"};
Ideas
* 1, sorted by array. Can be used to select, bubble
* 2, for nesting and comparison and transposition
* 3, the problem: the front row is an integer, compared with the arithmetic character
*
* String comparisons are using the CompareTo () function
*/
public class StringTest1 {
public static void Main (String[]args) {
String[] arr = {"NBA", "abc", "CBA", "ZZ", "QQ", "haha"};
System.out.print ("Array of characters before sorting:");
PrintArray (arr);
System.out.println ();
System.out.print ("sorted character array:");
Sortarray (arr);
PrintArray (arr);
}
public static void Sortarray (String arr[]) {
for (int x=0;x<arr.length-1;x++) {
for (int y=0;y<arr.length-1-x;y++) {
int num = Arr[y].compareto (arr[y+1]); String comparison with the CompareTo () function
if (num>0)
{
String s = arr[y];
ARR[Y]=ARR[Y+1];
Arr[y+1]=s;
}
}
}
}
public static void PrintArray (String arr[]) {
System.out.print ("[");
for (int i=0;i<arr.length;i++) {
if (i<arr.length-1) {
System.out.print (arr[i]+ ",");
}
else {
System.out.print (arr[i]+ "]");
}
}
}
}
1. Given a character array, sort from small to large in dictionary order