如何以list进行排序

import java.util.*;

public class TestSort {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		List<YuanShu> ys = new ArrayList<YuanShu>();

		YuanShu y1 = new YuanShu();
		y1.setT1(1324);
		y1.setT2(345);
		y1.setT3(436);
		ys.add(y1);

		YuanShu y2 = new YuanShu();
		y2.setT1(23);
		y2.setT2(8941);
		y2.setT3(431412);
		ys.add(y2);

		YuanShu y3 = new YuanShu();
		y3.setT1(786584);
		y3.setT2(23452);
		y3.setT3(43563);
		ys.add(y3);

		// 排序
		Collections.sort(ys, new SortByYuanShu ());

		for (YuanShu p : ys) {
			System.out.print(p.getT1() + "\t" + p.getT2() + "\t" + p.getT3());
			System.out.println();
		}
	}

}

class YuanShu {
	private int t1;
	private int t2;
	private int t3;

	public YuanShu() {
	}

	public int getT1() {
		return t1;
	}

	public void setT1(int t1) {
		this.t1 = t1;
	}

	public int getT2() {
		return t2;
	}

	public void setT2(int t2) {
		this.t2 = t2;
	}

	public int getT3() {
		return t3;
	}

	public void setT3(int t3) {
		this.t3 = t3;
	}
}

// 排序
@SuppressWarnings("unchecked")
class SortByYuanShu implements Comparator {
	public int compare(Object obj1, Object obj2) {
		YuanShu y1 = (YuanShu) obj1;
		YuanShu y2 = (YuanShu) obj2;
		if (y1.getT1() < y2.getT1())
			return 1;
		else
			return 0;
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值