集合中保存对象,如何按对象中的属性排序?



package testProject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class TestPro {

	public static void main(String[] args) {

		ArrayList<TestPro> zsList = new ArrayList<TestPro>();

		zsList.add(new TestPro("1", "0000"));
		zsList.add(new TestPro("Y", "1111"));
		zsList.add(new TestPro("Y", "1111444"));
		zsList.add(new TestPro("我是一个热好人", "2222"));
		zsList.add(new TestPro("S", "3333"));
		zsList.add(new TestPro("A", "4444"));
		zsList.add(new TestPro("I", "5555"));
		zsList.add(new TestPro("V", "6666"));
		zsList.add(new TestPro("Q", "7777"));
		zsList.add(new TestPro("I", "8888"));

		Collections.sort(zsList, new Comparator<TestPro>() {

			@Override
			public int compare(TestPro o1, TestPro o2) {
				// TODO Auto-generated method stub
				return o1.getAbbreviation().compareTo(o2.getAbbreviation());
			}

		});

		for (int i = 0; i < zsList.size(); i++) {
			System.out.println(zsList.get(i).getAbbreviation() + "==="
					+ zsList.get(i).getName());
		}

	}

	private String abbreviation;
	private String name;

	public TestPro() {
		super();
	}

	public TestPro(String abbreviation, String name) {
		super();
		this.abbreviation = abbreviation;
		this.name = name;
	}

	public String getAbbreviation() {
		return abbreviation;
	}

	public void setAbbreviation(String abbreviation) {
		this.abbreviation = abbreviation;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值