使用Arrays.sort()对自定义的entity按指定属性进行排序。

时间:2021-12-03 13:29:58

使用Arrays.sort()对自定义的entity按指定属性进行排序。

若想达到上面功能,自定义entity必须实现Comparable接口,然后重写compareTo()方法。

public class Employee implements Comparable<Employee>{
	private String name;
	private int age;
	
	public Employee(String n,int a){
		this.name = n;
		this.age = a;
	}
	
	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;
	}

	@Override
	public int <strong><span style="color:#FF0000;">compareTo</span></strong>(Employee o) {
		// TODO Auto-generated method stub
		int i = 0;
		if(this.age > o.age){<strong><span style="font-size:14px;color:#FF0000;"><span style="font-family:Verdana, Arial, Helvetica, sans-serif;">//这里比较的什么,sort()方法就按什么从小到大排序</span></span></strong>
			i = 1;
		}else if(this.age < o.age){
			i = -1;
		}
		
		return i;
	}
	
}


public class ArraysSortTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Employee []employees = new Employee[3];
		employees[0] = new Employee("yuying",34);
		employees[1] = new Employee("hanying",25);
		employees[2] = new Employee("laoyang",26);
		
		Arrays.sort(employees);
		
		for(Employee e:employees){
			System.out.println(e.getName() + "----" + e.getAge());
		}
	}

}


也可以这样:

public class ArraysSortTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Employee []employees = new Employee[4];
		employees[0] = new Employee("yuying",34);
		employees[1] = new Employee("hanying",25);
		employees[2] = new Employee("laoyang",26);
		employees[3] = new Employee("Dr.Cao",53);
		
//		Arrays.sort(employees);
		List<Employee> e_list = Arrays.asList(employees);
		Collections.sort(e_list);
		
		for(Employee e:employees){
			System.out.println(e.getName() + "----" + e.getAge());
		}
	}

}

附加一道Java面试题:

Comparable和Comparator接口是干什么的?列出它们的区别。

Java提供了只包含一个compareTo()方法的Comparable接口。这个方法可以个给两个对象排序。具体来说,它返回负数,0,正数来表明输入对象小于,等于,大于已经存在的对象。

Java提供了包含compare()和equals()两个方法的Comparator接口。compare()方法用来给两个输入参数排序,返回负数,0,正数表明第一个参数是小于,等于,大于第二个参数。equals()方法需要一个对象作为参数,它用来决定输入参数是否和comparator相等。只有当输入参数也是一个comparator并且输入参数和当前comparator的排序结果是相同的时候,这个方法才返回true。


与Comparator的区别

Comparator位于包java.util下,而Comparable位于包java.lang下,Comparable接口将比较代码嵌入自身类中,而后者在一个独立的类中实现比较。 如果类的设计师没有考虑到Compare的问题而没有实现Comparable接口,可以通过  Comparator来实现比较算法进行排序,并且为了使用不同的排序标准做准备,比如:升序、降序。


public class Contractor {
	private String name;
	private double salary;
	
	public Contractor(String n,double s){
		this.name = n;
		this.salary = s;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
	
	@Override
	public String toString(){ 
		return this.name + "----" + this.salary;
	}
}

专门实现一个比较器,这里按照Contractor实体的salary属性进行比较。

import java.util.Comparator;

public class ContractorComparator implements <span style="color:#FF0000;">Comparator</span><Contractor>{

	@Override
	public int <strong><span style="color:#FF0000;">compare</span></strong>(Contractor o1, Contractor o2) {
		// TODO Auto-generated method stub
		int i = 0;
		if(o1.getSalary() < o2.getSalary()){
			i = -1;
		}else if(o1.getSalary() > o2.getSalary()){
			i = 1;
		}
		return i;
	}
}

public class ContractorTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Contractor []contractors = new Contractor[4];
		contractors[0] = new Contractor("xiaowei",1100.0);
		contractors[1] = new Contractor("yuying",920.0);
		contractors[2] = new Contractor("mentoryang",1120.0);
		contractors[3] = new Contractor("Dr.Cao",5100.0);
		
		List<Contractor> c_list = Arrays.asList(contractors);
		
		ContractorComparator cc = new ContractorComparator();
		Collections.sort(c_list,cc);
		
		for(Contractor c:c_list){
			System.out.println(c);
		}
	}

}

也可以这样实现:

public class ContractorTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		ContractorComparator cc = new ContractorComparator();
		Set<Contractor> c_set = new TreeSet<Contractor>(<span style="color:#FF0000;"><strong>cc</strong></span>);//这里需要传一个参数,这个参数就是比较器
		c_set.add(new Contractor("xiaowei",1100.0));
		c_set.add(new Contractor("yuying",920.0));
		c_set.add(new Contractor("mentoryang",1120.0));
		c_set.add(new Contractor("Dr.Cao",5100.0));
		
		for(Contractor c:c_set){
			System.out.println(c);
		}
	}

}