import java.util.Arrays;
import java.util.Comparator;
public class CharArraySort {
public static void main(String[] args) {
String[] str = {"a","b","B","d","D","K","j","n","N","s"};
Arrays.sort(str,0,str.length,new Comparator<String>()
{
public int compare(String str1, String str2) {
String temp1=str1.toLowerCase();
String temp2=str2.toLowerCase();
if(!temp1.equals(temp2))
return temp1.compareTo(temp2);
else
return str1.compareTo(str2);
}
});
for(int i=0;i<str.length;i++)
System.out.print(str[i]+" ");
}
}
import java.util.Comparator;
public class CharArraySort {
public static void main(String[] args) {
String[] str = {"a","b","B","d","D","K","j","n","N","s"};
Arrays.sort(str,0,str.length,new Comparator<String>()
{
public int compare(String str1, String str2) {
String temp1=str1.toLowerCase();
String temp2=str2.toLowerCase();
if(!temp1.equals(temp2))
return temp1.compareTo(temp2);
else
return str1.compareTo(str2);
}
});
for(int i=0;i<str.length;i++)
System.out.print(str[i]+" ");
}
}
本文介绍了一个使用Java进行字符串数组自定义排序的例子。该程序通过Arrays类的sort方法结合匿名内部类实现忽略大小写的字符串排序,并在大小写相同的情况下保持原有顺序。通过这个例子可以了解如何在Java中实现复杂的排序逻辑。
653

被折叠的 条评论
为什么被折叠?



