JAVA 自定义比较器
参考链接:https://blog.csdn.net/whing123/article/details/77851737
Comparable
需要类实现Comparable接口,重写compareTo方法
Comparator
Comparator接口一般不会被集合元素类所实现,而是单独实现或者匿名内部类方式实现
重写compare()方法
Comparable
import java.util.*; public class RongQi { public static void main(String[] args) { Student s1 = new Student("Allen", 20, 1); Student s2 = new Student("Allen", 20, 45); Student s3 = new Student("Catalina", 40, 2); Student s4 = new Student("Diana", 30, 0); List<Student> l = new LinkedList(); l.add(s3); l.add(s1); l.add(s4); l.add(s2); Collections.sort(l);//对list排序 Iterator<Student> it = l.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } static class Student implements Comparable<Student> { // public class Student{ private String name; private int age; private int id; // public Student() { // } public Student(String name, int age, int id) { this.name = name; this.age = age; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", id=" + id + '}'; } @Override public int compareTo(Student s1) { int cmp = name.compareTo(s1.name);// 继续加入ID if (cmp != 0) { return cmp; } else { int cmp1 = age - s1.age; if (cmp1 != 0) { return cmp1; } else { return id - s1.id; } } } } }
Student{name='Allen', age=20, id=1}
Student{name='Allen', age=20, id=45}
Student{name='Catalina', age=40, id=2}
Student{name='Diana', age=30, id=0}
我一般用 Comparator
import java.util.*; public class Main { public static void main(String[] args) { Student s1 = new Student("Allen", 20, 1); Student s2 = new Student("Allen", 20, 45); Student s3 = new Student("Catalina", 40, 2); Student s4 = new Student("Diana", 30, 0); List<Student> l = new LinkedList(); l.add(s3); l.add(s1); l.add(s4); l.add(s2); Collections.sort(l, new StudentComparator());// 对list排序 Iterator<Student> it = l.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } static class Student { // public class Student{ private String name; private int age; private int id; public Student(String name, int age, int id) { this.name = name; this.age = age; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", id=" + id + '}'; } } static class StudentComparator implements Comparator { public int compare(Object o1, Object o2) { Student s1 = (Student) o1; Student s2 = (Student) o2; int cmp = s1.name.compareTo(s2.name);// 继续加入ID if (cmp != 0) { return cmp; } else { int cmp1 = s1.age - s2.age; if (cmp1 != 0) { return cmp1; } else { return s1.id - s2.id; } } } } }
Student{name='Allen', age=20, id=1}
Student{name='Allen', age=20, id=45}
Student{name='Catalina', age=40, id=2}
Student{name='Diana', age=30, id=0}
自定义数组排序
class Solution { static class StringComparator implements Comparator<String> { @Override public int compare(String s1, String s2) { //操作 return s1.compareTo(s2); } } public String minNumber(int[] nums) { String[] str = new String[nums.length]; String s =""; for (int i = 0; i < nums.length; i++) { str[i] = Integer.toString(nums[i]); } Arrays.sort(str, new StringComparator()); for(String ss:str){ s+=ss; } return s; } }