1. 程式人生 > >對字串陣列進行排序

對字串陣列進行排序

public class StringSort {

    public static void main(String[] args) {
        String[] s = {"abc","xyz","張三","123"};
        stringSort(s);
        print(s);
    }
    public static String[] stringSort(String[] s){
        //比較字串大小用compareTo()
        String t = null;
        //外迴圈:長度-1
        for(int
i=0;i<s.length-1;i++){ //內迴圈 : 長度-i-1 for(int j=0;j<s.length-i-1;j++){ if(s[j].compareTo(s[j+1]) > 0){ t =s [j]; s[j] = s[j+1]; s[j+1] = t; } } } return
s; } public static void print(String[] s){ for(String ss : s){ System.out.print(ss + "\t"); } } }