import java.util.Arrays;
import java.util.Comparator;
public class Test5 {
// 对一个String类型的数组进行排序
public static void main(String args[]) {
String[] str = new String[] { "2", "0", "23", "21", "17" };
Arrays.sort(str, new CustomComparator());
for (Object s : str) {
System.out.println(s);
}
}
}
class CustomComparator implements Comparator<String> {
public int compare(String str1, String str2) {
int name1 = Integer.parseInt(str1);
int name2 = Integer.parseInt(str2);
return name1 - name2;
}
}
用比较器去解决会比较方便一些! |