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; } }
集合中保存对象,怎样按对象中的属性排序?
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
List集合中的对象根据属性排序
集合类List存放的数据,默认是按照放入时的顺序存放的,比如依次放入A、B、C,则取得时候,则也是A、
List 排序 java 升序 ide