我是 ABin-阿斌:写一生代码,创一世佳话,筑一览芳华。 如果小伙伴们觉得我的文章不错,记得一键三连,感谢~
前言
- 在开发当中我们往往会接到这样的需求,就是让我们对一个列表(List)的多个字段进行排序。这个时候,我们把这个方法抽取成一个工具类,因为这种操作往往是项目全局通用的。
案例演示
工具类
package com.starry.ops.utils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
public class SortListUtil<E> {
public void sortByMethod(List<E> list, final String method,
final boolean reverseFlag) {
list.sort(new Comparator<Object>() {
@Override
@SuppressWarnings("unchecked")
public int compare(Object arg1, Object arg2) {
int result = 0;
try {
Method m1 = ((E) arg1).getClass().getMethod(method, null);
Method m2 = ((E) arg2).getClass().getMethod(method, null);
Object obj1 = m1.invoke(((E) arg1), null);
Object obj2 = m2.invoke(((E) arg2), null);
if (obj1 instanceof String) {
result = obj1.toString().compareTo(obj2.toString());
} else if (obj1 instanceof Date) {
long l = ((Date) obj1).getTime() - ((Date) obj2).getTime();
if (l > 0) {
result = 1;
} else if (l < 0) {
result = -1;
}
} else if (obj1 instanceof Integer) {
result = (Integer) obj1 - (Integer) obj2;
} else {
result = obj1.toString().compareTo(obj2.toString());
System.err.println("MySortList.sortByMethod方法接受到不可识别的对象类型,转换为字符串后比较返回...");
}
if (reverseFlag) {
result = -result;
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return result;
}
});
}
}
实体类
@Data
@ApiModel
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SecurityGroupListVo {
@ApiModelProperty("安全组Id(数据库中的Id)")
private String securityGroupId;
@ApiModelProperty("安全组名称")
private String securityGroupName;
@ApiModelProperty("vpc名称")
private String vpcName;
@ApiModelProperty("云服务器数量")
private Integer cloudServerNum;
@ApiModelProperty("裸金属服务器数量")
private Integer bareMetalServerNum;
@ApiModelProperty("GPU云服务器数量")
private Integer computerGpuNum;
@ApiModelProperty("GPU裸金属服务器数量")
private Integer computerGpuBareMetalNum;
@ApiModelProperty("专属宿主机服务器数量")
private Integer exclusiveComputerNum;
@ApiModelProperty("入站规则数量")
private Integer inboundRulesNum;
@ApiModelProperty("出站规则数量")
private Integer outboundRulesNum;
@ApiModelProperty("备注")
private String remark;
}
实际操作
List<SecurityGroupListVo> securityGroupVoList = new ArrayList<>();
SecurityGroupListVo securityGroupListVo = new SecurityGroupListVo()
.setSecurityGroupName(10)
.setCloudServerNum(20)
.setBareMetalServerNum(10)
.setComputerGpuNum(10)
.setComputerGpuBareMetalNum(10)
.setExclusiveComputerNum(10)
.setInboundRulesNum(10);
securityGroupVoList.add(securityGroupListVo);
SortListUtil<SecurityGroupListVo> groupListVoSortListUtil = new SortListUtil<>();
groupListVoSortListUtil.sortByMethod(securityGroupVoList, "getCloudServerNum", true);
groupListVoSortListUtil.sortByMethod(securityGroupVoList, "getBareMetalServerNum", true);
groupListVoSortListUtil.sortByMethod(securityGroupVoList, "getComputerGpuNum", true);
groupListVoSortListUtil.sortByMethod(securityGroupVoList, "getComputerGpuBareMetalNum", true);
groupListVoSortListUtil.sortByMethod(securityGroupVoList, "getExclusiveComputerNum", true);
groupListVoSortListUtil.sortByMethod(securityGroupVoList, "getInboundRulesNum", true);
groupListVoSortListUtil.sortByMethod(securityGroupVoList, "getOutboundRulesNum", true);
System.out.println("排序结果:"+securityGroupVoList);
总结
- 如果还是没明白可以观看这篇文章:https://blog.csdn.net/superdog007/article/details/54133698
|