1. 自定义Comparator排序
public static void main(String[] args) {
List<FormDataDetail> list = new ArrayList<>();
FormDataDetail detail1 = new FormDataDetail();
detail1.setContent("1");
detail1.setDataIndex(1l);
FormDataDetail detail2 = new FormDataDetail();
detail2.setContent("2");
detail2.setDataIndex(2l);
FormDataDetail detail3 = new FormDataDetail();
detail3.setContent("3");
detail3.setDataIndex(3l);
FormDataDetail detail4 = new FormDataDetail();
detail4.setContent("4");
detail4.setDataIndex(4l);
list.add(detail1);
list.add(detail3);
list.add(detail2);
list.add(detail4);
FormDataDetail [] a = list.toArray(new FormDataDetail[list.size()]);
for (FormDataDetail formDataDetail : list) {
System.out.println(formDataDetail.getContent());
}
System.out.println("=======================");
Arrays.sort(a, new Comparator<FormDataDetail>() {
@Override
public int compare(FormDataDetail o1, FormDataDetail o2) {
// System.out.println("o1 =>" + o1.getDataIndex() + " o2 =>" + o2.getDataIndex() + " ");
// System.out.println(o1.getDataIndex() > o2.getDataIndex());
return o1.getDataIndex() > o2.getDataIndex() ? 0 : -1;
}
});
for (FormDataDetail formDataDetail : a) {
System.out.println(formDataDetail.getContent());
}
}