Java中的Comparable、Comparator排序问题

在收集对象之后,对对象进行排序是常用的动作,你不用亲自操作排序算法,在Java中有个Collections的类提供有sort()方法。由于必须有索引才能进行排序,因此Collections的sort()方法接受List操作对象。列如:

public class Demo {
	public static void main(String[] args) {
		List<Integer>num=Arrays.asList(10,8,9,5,3,6);
		Collections.sort(num);
		System.out.println(num);
	}
}

以上的例子是默认按数字的大小来自然排序的,但是有时候我们并不想按默认的顺序来进行排序,这个时候我们就要重新自定义排序了,在Java SE中提供了Comparable、Comparator两个接口来自定义排序,其中Comparable是排序接口,而Comparator是比较接口。

  1. 操作Comparable
    Comparable是排序接口,相当于“内部比较器”。若一个类实现了Comparable接口,就意味着该类支持排序,直接就成为一个可以比较的对象,但是需要修改源代码。Collections的sort()方法要求被排序的对象,必须操作Comparable接口,这个接口有个compareTo()方法,是比较此对象与指定对象的顺序,如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。
    第一、二步:实现接口Comparable并且实现compareTo方法:
//第一步 :实现接口Comparable<T>
class Account implements Comparable<Account> {
	private String name;
	private String number;
	private int balance;
	public Account(String name, String number, int balance) {
		super();
		this.name = name;
		this.number = number;
		this.balance = balance;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public int getBalance() {
		return balance;
	}
	public void setBalance(int balance) {
		this.balance = balance;
	}
	@Override
	//第二步 实现compareTo方法 o 表示和当前对象比较的另外一个对象
	public int compareTo(Account o) {
		// 从小到大 :this-o
		// 从大到小:o-this
		// 先根据余额排序,如果余额一样,根据姓名排序
		if (this.balance != o.balance) {
			return this.balance - o.balance;
		} else {
			return this.name.compareTo(o.name);
		}
	}
}
 第三步:调用sort排序:
public class Sort {
	public static void main(String[] args) {
        List<Account>accounts =new ArrayList<>();
        accounts.add(new Account("A", "5", 1500));
        accounts.add(new Account("C", "5", 1400));
        accounts.add(new Account("D", "5", 1200));
        accounts.add(new Account("B", "5", 1200));
        accounts.add(new Account("E", "5", 1800));
        //第三步 调用sort 排序	
        Collections.sort(accounts);
        //使用迭代器遍历输出列表数据
        Iterator<Account> it= accounts.iterator();
        while(it.hasNext()){
        	Account account=it.next();
        	System.out.println(account.getName()+"\t"+account.getNumber()+"\t"+account.getBalance());
        }
	}
}

示例代码执行结果:
在这里插入图片描述
说明:Collections的sort()方法在取得a对象与b对象进行比较时,会先将a对象扮演为Comparable,然后调用a.compareTo(b),如果a对象顺序小于b对象则返回小于0的值,若顺序相等则返回0,若顺序上a大于b则返回大于0的值。因此,上面列子中,将会依余额从小到大来排序,如果余额相等时则依姓名排序。
2. 操作Comparator
Comparator是比较接口,相当于“外部比较器”,是不需要修改源代码, 而是另外实现一个比较器, 当某个自定义的对象需要作比较的时候,把比较器和对象一起传递过去就可以比大小了。我们如果需要控制某个类的次序,而该类本身不支持排序(即没有实现Comparable接口),这个时候我们要建立一个“该类的比较器”,实现Comparator接口即可。
第一步:写出要比较的类

class Account1 {
	private String name;
	private String number;
	private int balance;
	public Account1(String name, String number, int balance) {
		super();
		this.name = name;
		this.number = number;
		this.balance = balance;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public int getBalance() {
		return balance;
	}
	public void setBalance(int balance) {
		this.balance = balance;
	}
}

第二步:调用sort()方法排序,实现接口Comparator中的 compare()的方法

public class Sort1 {
	public static void main(String[] args) {
		List<Account1> accounts = new ArrayList<>();
		accounts.add(new Account1("A", "5", 1500));
		accounts.add(new Account1("C", "5", 1400));
		accounts.add(new Account1("D", "5", 1200));
		accounts.add(new Account1("B", "5", 1200));
		accounts.add(new Account1("E", "5", 1800));
		  // 调用sort 排序
		Collections.sort(accounts, new Comparator<Account1>() {
			@Override
			public int compare(Account1 o1, Account1 o2) {
				// 从小到大:o1-o2 从大到小:o2-o1
				// 先根据余额排序,如果余额一样,根据姓名排序
				if (o1.getBalance() != o2.getBalance()) {
					return o1.getBalance() - o2.getBalance();
				} else {
					return o1.getName().compareTo(o2.getName());
				}
			}
		});
		 //使用迭代器遍历输出列表数据
		Iterator<Account1>it=accounts.iterator();
		while(it.hasNext()){
			Account1 account1=it.next();
			System.out.println(account1.getName()+"\t"+account1.getNumber()+"\t"+account1.getBalance());
		}
	}
}

示例代码执行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值