对字符串数组进行排序,在冒泡法排序中使用compareTo()方法确定排序的顺序。

//A bubble sort for Strings.
public class SortString
{
 static String  arr[] = {"NOP","DEF","GHI","JK","LM","ABC","abc","Abc"};
 public static void main(String[] args)
 {
  for(int i=0;i<arr.length;i++){
   for(int j=i+1;j<arr.length;j++){
    if(arr[j].compareTo(arr[i])<0){
    //if(arr[j].compareToIgnoreCase(arr[i])<0){  //忽略大小写
     String t = arr[i];
     arr[i] = arr[j];
     arr[j] = t;
    }
   }
   System.out.println(arr[i]);
  }
  System.out.println("The End!");
 }
}

你可能感兴趣的:(J#)