对一个List<OutputAttributeBean> outputAttrs 排序时, 可以根据OutputAttributeBean中的属性来新建一个 自定义的Comparator。
而不需要将OutputAttributeBean 实现Comparator接口, 这样能起到解耦的作用。
而不需要将OutputAttributeBean 实现Comparator接口, 这样能起到解耦的作用。
public class OutputAttributeBean {
private String name;
private String value;
private String index;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
}
public class CollectionSortUtil implements Comparator {
public int compare(Object arg0, Object arg1) {
OutputAttributeBean property0 = (OutputAttributeBean) arg0;
OutputAttributeBean property1 = (OutputAttributeBean) arg1;
int index1 = Integer.valueOf(property0.getIndex());
int index2 = Integer.valueOf(property1.getIndex());
return index1 - index2;
}
}
CollectionSortUtil comparator = new CollectionSortUtil();
Collections.sort(outputAttrs, comparator);