class Demo_15 {
public static void main(String[] args) {
//创建一个字符串数组
String[] strArr = {"sdkjf","deadg","daacxz","jietl","adsfd"};
//排序前遍历字符串数组
printStrArr(strArr);
//排序
sortArr(strArr);
//遍历数组
printStrArr(strArr);
}
//定义遍历数组的方法
public static void printStrArr(String[] arr){
for(int i = 0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
//定义字符串排序的方法
public static void sortArr(String[] arr){
for(int i=0;i<arr.length;i++){
for(int j = i+1;j<arr.length;j++){
//若字符串比要比较的字符串大,则返回一个正数
int n = arr[i].compareTo(arr[j]);
if(n>0){
swap(arr,i,j);
}
}
}
}
//交换
public static void swap(String[] arr,int a,int b){
String temp = arr[a];
arr[a] =arr[b];
arr[b] = temp;
}
}