java list<string> 排序_Java对List(Date,String,int)对象进行排序工具类

@param sortMode 排序方式:ASC,DESC 任选其一 */

public static void sortList(List list, final String sortField, final String sortMode) {

if(list == null || list.size() < 2) {

return;

}

Collections.sort(list, new Comparator() {

@Override

public int compare(T o1, T o2) {

try {

Class clazz = o1.getClass();

Field field = clazz.getDeclaredField(sortField); //获取成员变量

field.setAccessible(true); //设置成可访问状态

String typeName = field.getType().getName().toLowerCase(); //转换成小写 Object v1 = field.get(o1); //获取field的值

Object v2 = field.get(o2); //获取field的值

boolean ASC_order = (sortMode == null || "ASC".equalsIgnoreCase(sortMode));

//判断字段数据类型,并比较大小

if(typeName.endsWith("string")) {

String value1 = v1.toString();

String value2 = v2.toString();

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else if(typeName.endsWith("short")) {

Short value1 = Short.parseShort(v1.toString());

Short value2 = Short.parseShort(v2.toString());

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else if(typeName.endsWith("byte")) {

Byte value1 = Byte.parseByte(v1.toString());

Byte value2 = Byte.parseByte(v2.toString());

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else if(typeName.endsWith("char")) {

Integer value1 = (int)(v1.toString().charAt(0));

Integer value2 = (int)(v2.toString().charAt(0));

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else if(typeName.endsWith("int") || typeName.endsWith("integer")) {

Integer value1 = Integer.parseInt(v1.toString());

Integer value2 = Integer.parseInt(v2.toString());

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else if(typeName.endsWith("long")) {

Long value1 = Long.parseLong(v1.toString());

Long value2 = Long.parseLong(v2.toString());

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else if(typeName.endsWith("float")) {

Float value1 = Float.parseFloat(v1.toString());

Float value2 = Float.parseFloat(v2.toString());

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else if(typeName.endsWith("double")) {

Double value1 = Double.parseDouble(v1.toString());

Double value2 = Double.parseDouble(v2.toString());

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else if(typeName.endsWith("boolean")) {

Boolean value1 = Boolean.parseBoolean(v1.toString());

Boolean value2 = Boolean.parseBoolean(v2.toString());

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else if(typeName.endsWith("date")) {

Date value1 = (Date)(v1);

Date value2 = (Date)(v2);

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else if(typeName.endsWith("timestamp")) {

Timestamp value1 = (Timestamp)(v1);

Timestamp value2 = (Timestamp)(v2);

return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

}

else {

//调用对象的compareTo()方法比较大小

Method method = field.getType().getDeclaredMethod("compareTo", new Class[]{field.getType()});

method.setAccessible(true); //设置可访问权限

int result = (Integer)method.invoke(v1, new Object[]{v2});

return ASC_order ? result : result*(-1);

}

}

catch (Exception e) {

String err = e.getLocalizedMessage();

System.out.println(err);

e.printStackTrace();

}

return 0; //未知类型,无法比较大小

}

});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤来实现: 1. 定义一个Map来保存每个月份的订单金额,月份作为key,金额作为value。 2. 遍历订单列表ordersList,将每个订单的订单金额累加到对应月份的value中。 3. 最后将Map中的数据按照月份排序,并将月份和订单金额封装成一个Map,添加到List中。 4. 使用CollUtil.newArrayList方法将List输出。 下面是代码示例: ```java import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import com.google.common.collect.Maps; import java.math.BigDecimal; import java.util.Date; import java.util.List; import java.util.Map; public class OrderUtils { public static List<Map<String, Object>> groupByMonth(List<Orders> ordersList) { Map<Integer, BigDecimal> monthMap = Maps.newHashMap(); for (Orders orders : ordersList) { Date paymentTime = DateUtil.parse(orders.getPaymentTime(), "yyyy-MM-dd HH:mm:ss"); int month = DateUtil.month(paymentTime) + 1; BigDecimal orderPrice = new BigDecimal(orders.getOrderPrice()); if (monthMap.containsKey(month)) { monthMap.put(month, monthMap.get(month).add(orderPrice)); } else { monthMap.put(month, orderPrice); } } List<Map<String, Object>> result = CollUtil.newArrayList(); for (int i = 1; i <= 12; i++) { Map<String, Object> map = Maps.newHashMap(); map.put("month", i); map.put("orderPrice", monthMap.containsKey(i) ? monthMap.get(i) : BigDecimal.ZERO); result.add(map); } return result; } } ``` 在上面的代码中,groupByMonth方法接收一个订单列表ordersList作为参数,返回一个List<Map<String, Object>>,其中每个Map保存了月份和订单金额。我们使用了Hutool和Guava库提供的工具类来简化代码,例如Hutool的DateUtil和CollUtil,Guava的Maps。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值