按照ArrayList指定的某一列进行排序

按照ArrayList中指定的某一列进行排序

private void sortArrayList(ArrayList nameAndWeight,final int index) {
//        
        Collections.sort(nameAndWeight, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                ArrayList list1 = (ArrayList) o1;
                ArrayList list2 = (ArrayList) o2;
                int compare =0 ;
                if(((Double) list1.get(index) - (Double) list2
                        .get(index))>0){
                    compare = 1;
                }else if(((Double) list1.get(index) - (Double) list2
                        .get(index))<0){
                    compare = -1;
                }                
                return compare;
            }
        });
    }

新写一个比较器Comparator(Object o1,Object o2);

posted on 2013-05-30 17:12  ywf—java  阅读(553)  评论(0编辑  收藏  举报

导航