package t23;
//字符串排序.
public class Test {
public static void main(String[] args) {
String[] strs= {"dog","cat","apple","app","APP","application","ball"};
for(String s:sort(strs)) {
System.out.println(s);
}
}
public static String[] sort(String[] strs) {
for(int i=0;i<strs.length-1;i++) {
for(int j=0;j<strs.length-1-i;j++) {
if(isLarger(strs[j], strs[j+1])) {
String temp=strs[j];
strs[j]=strs[j+1];
strs[j+1]=temp;
}
}
}
return strs;
}
private static boolean isLarger(String str1,String str2) {
int length=str1.length()<str2.length()?str1.length():str2.length();
for(int i=0;i<length;i++) {
if(str1.charAt(i)>str2.charAt(i)) {
return true;
}else if(str1.charAt(i)<str2.charAt(i)) {
return false;
}else {
continue;
}
}
return str1.length()<str2.length()?false:true;
}
}
Java_字符串排序
最新推荐文章于 2024-08-05 02:07:41 发布