Java中的排序

    Java的排序从“容器”类型来看,主要包括两种类型:一是数组排序;二是collection排序。

    从被排序的元素来看,又主要分为两类:一是基本数据类型的排序;而是自定义对象的排序。

    而对于自定义对象的排序,有两种实现方法:一是自定义一个比较器,即实现Comparator接口,不具有侵入性;二是让被排序对象实现Comparable接口,具有侵入性。

1 基本数据类型的数组排序

		String[]  strs={"Asd","DC","dc","sad","SAD","asdf"};
		Arrays.sort(strs);
		System.out.println(Arrays.toString(strs));
		//第二个参数是comparator
		Arrays.sort(strs,Collections.reverseOrder());
		System.out.println(Arrays.toString(strs));
		//忽略大小写
		Arrays.sort(strs, String.CASE_INSENSITIVE_ORDER);
		System.out.println(Arrays.toString(strs));
		
		Integer[] nums={4,12,3,1};
		Arrays.sort(nums);
		System.out.println(Arrays.toString(nums));
		//第二个参数是comparator
		Arrays.sort(nums, Collections.reverseOrder());
		System.out.println(Arrays.toString(nums));
运行结果:
[Asd, DC, SAD, asdf, dc, sad]
[sad, dc, asdf, SAD, DC, Asd]
[Asd, asdf, dc, DC, sad, SAD]
[1, 3, 4, 12]
[12, 4, 3, 1]

2 基本数据类型的Collection排序

		List<Integer> list=new ArrayList<Integer>();
		list.add(6);
		list.add(12);
		list.add(3);
		
		Collections.sort(list);
		System.out.println(list);
		//第二个参数是Comparator
		Collections.sort(list, Collections.reverseOrder());
		System.out.println(list);

运行结果:
[3, 6, 12]
[12, 6, 3]

3 自定义对象的排序

3.1 使用comparator接口,自定义比较器

被比较的对象:
public class Coffee {
	private int price;
	private String brand;
	public Coffee(int price,String brand) {
		// TODO Auto-generated constructor stub
		this.price=price;
		this.brand=brand;
	}
	public int getPrice() {
		return price;
	}
	public String getBrand() {
		return brand;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return brand+"  "+price;
	}
}
自定义比较器:
import java.util.Comparator;

public class CoffeeComparator implements Comparator<Coffee> {

	@Override
	public int compare(Coffee o1, Coffee o2) {
		// 先按照价格排序,从低到高,然后再按照名称排序,升序
		if (o1.getPrice()!=o2.getPrice()) {
			return o1.getPrice()-o2.getPrice();
		}else {
			return o1.getBrand().compareTo(o2.getBrand());
		}
	}

}
测试代码:
这种方法其实使用了策略模式的思想,通过传入不同的比较器,来实现不同的排序。
		List<Coffee> coffees=new ArrayList<Coffee>();
		Coffee mocha=new Coffee(12, "mocha");
		Coffee latte=new Coffee(10, "latte");
		Coffee capu=new Coffee(10, "copu");
		Coffee breve=new Coffee(15, "breve");
		coffees.add(breve);
		coffees.add(latte);
		coffees.add(mocha);
		coffees.add(capu);
		Collections.sort(coffees, new CoffeeComparator());
		System.out.println(coffees);
运行结果:
[copu  10, latte  10, mocha  12, breve  15]

3.2 实现comparable接口,具有一定侵入性

被比较对象:
public class Coffee implements Comparable<Coffee>{
	private int price;
	private String brand;
	public Coffee(int price,String brand) {
		// TODO Auto-generated constructor stub
		this.price=price;
		this.brand=brand;
	}
	public int getPrice() {
		return price;
	}
	public String getBrand() {
		return brand;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return brand+"  "+price;
	}
	@Override
	public int compareTo(Coffee o) {
		// TODO Auto-generated method stub
		if (this.getPrice()!=o.getPrice()) {
			return o.getPrice()-this.getPrice();
		}else {
			return this.getBrand().compareTo(o.getBrand());
		}
	}
}
测试代码:
		List<Coffee> coffees=new ArrayList<Coffee>();
		Coffee mocha=new Coffee(12, "mocha");
		Coffee latte=new Coffee(10, "latte");
		Coffee capu=new Coffee(10, "copu");
		Coffee breve=new Coffee(15, "breve");
		coffees.add(breve);
		coffees.add(latte);
		coffees.add(mocha);
		coffees.add(capu);
		
		Collections.sort(coffees);
		
		System.out.println(coffees);
运行结果:
[breve  15, mocha  12, copu  10, latte  10]



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值