我试图更改以下代码中的排序方法以对任何类型的对象进行排序,而不仅仅是字符串,但有点难过,欢迎所有建议。
class SortNames {
static void sort(String[] s) {
for (int i = 0; i<s.length; i++) {
for (int j = i+1; j < s.length; j++) {
if (s[i].compareTo(s[j])>0) {
String t;
t = s[i]; s[i] = s[j]; s[j] = t;
}
}
}
}
public static void main(String[] args) {
String[] names = {"Pete","Jill","May","Anne","Tim"};
sort(names);
for (int i=0; i<names.length; i++)
System.out.println(names[i]);
}
}