我想根据名称属性对包含 MyBean 对象的列表进行排序,而无需触及 MyBean 类
我有一个像
public class MyBean
{
private String name, city;
public void setName(String name)
{
this.name = name;
}
public void setCity(String city)
{
this.city= city;
}
public String getName()
{
return name;
}
public String getCity()
{
return city;
}
}
我还有另一堂课,比如,
public class MyCollection
{
p s v main(String args[])
{
MyBean bean1 = new MyBean();
MyBean bean2 = new MyBean();
bean1.setName("arun");
bean1.setCity("xyz");
bean2.setName("raju");
bean2.setCity("abc");
bean2.setName("bharani");
bean2.setCity("abcfr");
ArrayList list = new ArrayList():
list.add(bean1);
list.add(bean2);
}
}
排序后应打印列表名称,如 arun bharani raju