1:数组排序
String[] str = new String[]{“china”,”guangzhou”,”beijing”};
System.out.println(Arrays.toString(str));
Arrays.sort(str);
System.out.println(Arrays.toString(str));
2:数组对象里属性排序
2.1 使用Comparable接口
public classs Student implements Comparable<Student>{
private int id;
private int score;
@Override
public int compareTo(Student stu) {
return Integer.valueOf(stu.score).compareTo(Integer.valueOf(this.score));
}
}
Student[] stus = new Student[]{
new Student("1001",90),
new Student("1002",70),
new Student("1003",100),
new Student("1004",50),
}
Arrays.sort(stus);
2.2 使用Comparator接口
public classs Student{
private int id;
private int score;
}
Student[] stus = new Student[]{
new Student("1001",90),
new Student("1002",70),
new Student("1003",100),
new Student("1004",50),
}
Arrays.sort(stus, new Comparator<Student>() {
@Override
public int compare(Student stu1, Student stu2) {
return Integer.valueOf(stu1.score).compareTo(Integer.valueOf(stu2.score));
}
});
================分隔线========================
1:集合排序
Collections.sort():
List<String> list = new ArrayList<String>();
list.add("你好!");
list.add("guangzhou");
list.add("china");
list.add("beijing");
System.out.println(list.toString());
Collections.sort(list);
System.out.println(list);
1.1:使用Comparator接口排序,同1
List<String> list = new ArrayList<String>();
list.add("你好!");
list.add("guangzhou");
list.add("china");
list.add("beijing");
System.out.println("原順序:"+list);
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);//-1、0、1
}
});
System.out.println("自定義順序:"+list);
Collections.reverse(list);
System.out.println("然後再反序:"+list);
2:集合里的对象属性排序
2.1 使用Comparator接口
public classs Student{
private int id;
private int score;
}
List<Student> list = new ArrayList<Student>();
list.add(new Student("1001",90));
list.add(new Student("1002",70));
list.add(new Student("1003",100));
list.add(new Student("1004",50));
System.out.println("原順序:"+list);
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student stu1, Student stu2) {
return Integer.valueOf(stu1.score).compareTo(Integer.valueOf(stu2.score));
}
});
System.out.println("自定義順序:"+list);
Collections.reverse(list);
System.out.println("然後再反序:"+list);
2.2 Comparable
public classs Student implements Comparable<Student>{
private int id;
private int score;
@Override
public int compareTo(Student stu) {
return Integer.valueOf(stu.score).compareTo(Integer.valueOf(this.score));
}
}
List<Student> list = new ArrayList<Student>();
list.add(new Student("1001",90));
list.add(new Student("1002",70));
list.add(new Student("1003",100));
list.add(new Student("1004",50));
System.out.println("原順序:"+list);
Collections.sort(list);
System.out.println("自定義順序:"+list);
2.3 扩展:TreeSet集合是有序集合(与add顺序不一致)
public classs Student implements Comparable<Student>{
private int id;
private int score;
@Override
public int compareTo(Student stu) {
return Integer.valueOf(stu.score).compareTo(Integer.valueOf(this.score));
}
}
TreeSet ts = new TreeSet<Student>();
ts.add(new Student("1001",90));
ts.add(new Student("1002",70));
ts.add(new Student("1003",100));
ts.add(new Student("1004",50));
结果为按照score排序的有序集合
compareTo方法原理
比较A、B连个int类型数字,A<B:-1、A=B:0、A>B:1
if(A<B){
ruturn -1;
}else if(A==B){
return 0;
}else{
return 1;
}
改为三元运算符
A<B?-1:(A>B?1:0)
如果int改为Integer包装类型
A.compareTo(B)
总结:
Comparator对代码没有任何污染,耦合性低
Comparable对代码有污染,耦合性高