当对象放入Vector,Arraylist等集合里时是没有顺序的,如果想让对象按照某种顺序输出,我们该怎么办呢?
第一用
Comparator接口,让类对象实现此接口的唯一方法:compare( Object o1, Object o2 ),然后再调 Collections.sort(list, comp);
但Commons下的一句话就可以让List中的对象按指定的类属性排序,如下:
Collections.sort(li, new BeanComparator("billIndex"));
当然也可以这样来定义倒叙,顺序等:
Comparator mycmp = ComparatorUtils.naturalComparator();
Comparator cmp = new BeanComparator("billIndex", mycmp);
Collections.sort(li, cmp);
ComparatorUtils类下有我们需要的排序方法!!