paulwong

自定义集合中的排序

1、先自定义Comparator


//    对象的比较器,如1.1.1<1.2.1<1.11.1<1.1.1.1
    public final class LcsjMapComparator implements Comparator
    {

        
        
public int compare(Object arg0, Object arg1) {
            GdnkVO_Lcsj_Map vo1
=(GdnkVO_Lcsj_Map)arg0;
            GdnkVO_Lcsj_Map vo2
=(GdnkVO_Lcsj_Map)arg1;
            String[] flowCode1
=vo1.getFlowCode().trim().split("\\.");
            String[] flowCode2
=vo2.getFlowCode().trim().split("\\.");

            
for(int i=0;i<2;i++)
            {
                
if(Integer.parseInt(flowCode1[i])<Integer.parseInt(flowCode2[i]))
                    
return -1;
                
else if(Integer.parseInt(flowCode1[i])>Integer.parseInt(flowCode2[i]))
                    
return 1;
            }
            
return 0;
        }
        
    }    


compare方法中的对象类型必须是集合中的元素的数据类型。

排在后面的返回1,排在前面的返回-1,相同顺序的返回0。


2、使用


Comparator lcsjComparator=new LcsjMapComparator();
Collections.sort(lcsjList,lcsjComparator);


即可得到排好序的集合。

posted on 2006-05-19 11:33 paulwong 阅读(313) 评论(0)  编辑  收藏 所属分类: J2EE


只有注册用户登录后才能发表评论。


网站导航: