字符串排序Java

题目:

字符串排序。

public class stringSort{
    
    
	public static void main(String[] args) {
    
    
		int N=5;
		String temp = null;
		String[] s = new String[N];
		s[0] = "matter";
		s[1] = "state";
		s[2] = "solid";
		s[3] = "liquid";
		s[4] = "gas";
		for(int i=0; i<N; i++) {
    
    
			for(int j=i+1; j<N; j++) {
    
    
				if(compare(s[i], s[j]) == false) {
    
    
					temp = s[i];
					s[i] = s[j];
					s[j] = temp;
				}
			}
		}
		for(int i=0; i<N; i++) {
    
    
			System.out.println(s[i]);
		}
	}
	//字符串比较
	static boolean compare(String s1, String s2) {
    
    
		boolean result = true;
		for(int i=0; i<s1.length() && i<s2.length(); i++) {
    
    
			if(s1.charAt(i) > s2.charAt(i)) {
    
    
				result = false; break;
			} else if(s1.charAt(i) <s2.charAt(i)) {
    
    
				result = true;
				break;
			} else {
    
    
			if(s1.length() < s2.length()) {
    
    
				result = true;
			} else {
    
    
				result = false;
			}
		}
	}
	return result;
	}
}

猜你喜欢

转载自blog.csdn.net/p715306030/article/details/113992244