1、先自定义Comparator
compare方法中的对象类型必须是集合中的元素的数据类型。
排在后面的返回1,排在前面的返回-1,相同顺序的返回0。
2、使用
即可得到排好序的集合。
//
对象的比较器,如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 ;
}
}
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);
Collections.sort(lcsjList,lcsjComparator);
即可得到排好序的集合。