创建需要比较的实体属性
字符创比较
public class ZoneComparatorName implements Comparator<ZoneDTO> {
@Override
public int compare(ZoneDTO Z1, ZoneDTO Z2) {
String name1 = Z1.getName();
String name2 = Z2.getName();
if (name1.compareTo(name2) > 0)
{
return 0;
}
else
{
return -1;
}
}
}
整形比较
public class ZoneComparatorStreet implements Comparator<ZoneDTO> {
@Override
public int compare(ZoneDTO Z1, ZoneDTO Z2) {
int age1 = Integer.parseInt(Z1.getStreet());
int age2 = Integer.parseInt(Z2.getStreet());
if (age1 > age2)
{
return 0;
}
else
{
return -1;
}
}
}
创建需要比较的实体
public class ZoneDTO {
// 名称
private String name;
private String street;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
进行属性比较
Result为list< ZoneDTO >
ZoneComparatorName pComparatorStreet = new ZoneComparatorName();
Collections.sort(result, pComparatorStreet);