字符串排序
2014-07-06 13:42
225 查看
public static void main(String[] args) {
String temp = null;
String[] s = new String[5];
s[0] = "china".toLowerCase();
s[1] = "apple".toLowerCase();
s[2] = "MONEY".toLowerCase();
s[3] = "BOOk".toLowerCase();
s[4] = "yeah".toLowerCase();
for(int i=0; i<s.length; i++) {
for(int j=i+1; j<s.length; j++) {
if(compare(s[i], s[j]) == false) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
for(int i=0; i<s.length; 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;
}
}
String temp = null;
String[] s = new String[5];
s[0] = "china".toLowerCase();
s[1] = "apple".toLowerCase();
s[2] = "MONEY".toLowerCase();
s[3] = "BOOk".toLowerCase();
s[4] = "yeah".toLowerCase();
for(int i=0; i<s.length; i++) {
for(int j=i+1; j<s.length; j++) {
if(compare(s[i], s[j]) == false) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
for(int i=0; i<s.length; 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;
}
}
相关文章推荐
- 任意输入一个字符串,将其中的字符按ASCII码值从小到大重新排序
- 作个把字符插入到一个按升序排序的字符串中的方法
- acm 试题 字符串排序
- javascript 字符串简单排序
- [python]一行搞定字符串排序
- Hibernate不支持Oracle对字符串排序后的基于rownum的取数方式
- 字符串排序的程序代码 acm试题
- 字符串技巧排序
- 按拼音或者笔画排序你的字符串
- 按不同国家语言进行字符串排序
- 按照字符串排序的Sql写法
- java中文字符串按拼音排序 代码
- 字符串排序输出函数
- STL对字符串排序
- 按不同国家语言进行字符串排序 选自:Goodspeed's Blog
- 字符串排序
- search花絮之文本字符串搜索结果的排序
- 获取字符串的首字母(包括中文),应用于按26个字母过滤和排序!
- 算法记录:MSD基数排序 + qsort 对字符串排序
- java中文字符串按拼音排序 代码(转)