字符串排序

题目:字符串排序。
String.compareTo(String anotherString):
比较两个字符串按c的unicode 来比较,用String 的每一个字符从头依次比较,
比较是用StringChar - anotherStrhingChar 

    public class Test{
        //比较两个字符串如果str1>=str2 为true
        public boolean compare(String str1,String str2){
            int i=str1.compareTo(str2);
            return i>=0;
        }
        public static void main(String args[]){
            String[] strs = {"matter","state","solid","liquid","gas"};
            String temp;
            Test t = new Test();
            int len = strs.length;
            for(int i=0;i<len;i++){
                for(int j=0;j<len-i;j++){
                    if(j+1<len-i){
                        if(t.compare(strs[j],strs[j+1])){
                            temp=strs[j];
                            strs[j]=strs[j+1];
                            strs[j+1]=temp;
                        }
                    }
                    
                }
            }
            for(String str : strs)
            System.out.print(str+" ");
        }
    }
原文地址:https://www.cnblogs.com/laoquans/p/2963342.html