Closed venusdrogon closed 5 years ago
//设置productType
try{
Collections.sort(
itemType,
new FixedOrderComparator<>(
StoPropertyConstants.PRPT_ITEM_HYPELAUNCH,
StoPropertyConstants.PRPT_ITEM_MIADIDAS_VALUE_EN,
StoPropertyConstants.PRPT_ITEM_PRESONALLZATION_CODE,
StoPropertyConstants.PRPT_ITEM_PERSALES_CODE,
StoPropertyConstants.PRPT_ITEM_VIP_CODE,
StoPropertyConstants.PRPT_ITEM_COMINGSOON_CODE,
StoPropertyConstants.PRPT_ITEM_DISCOUNT_CODE,
StoPropertyConstants.PRPT_ITEM_NORMAL_CODE,
StoPropertyConstants.PRPT_ITEM_NOSALE));
}catch (Exception e){
LOGGER.error("itemType sort error:{},itemType:{}", e, JsonUtil.format(itemType));
}
[2019-07-28 16:11:31:692 CST] ERROR com.**.store.manager.product.StoRecommendManagerImpl.getRecommendItemCommands(StoRecommendManagerImpl.java:436) : itemType sort error:java.lang.IllegalArgumentException: Attempting to compare unknown object Sale,itemType: [
"Normal",
"Sale"
]
[2019-07-28 16:11:32:776 CST] ERROR com.**.store.manager.product.StoRecommendManagerImpl.getRecommendItemCommands(StoRecommendManagerImpl.java:436) : itemType sort error:java.lang.IllegalArgumentException: Attempting to compare unknown object Sale,itemType: [
"Normal",
"Sale"
]
对 集合 list 按照指定的固定顺序 fixedOrderItems 进行排序.
说明: 默认使用的是 UnknownObjectBehavior.AFTER ,不在指定固定顺序的元素将排在后面 示例: assertThat( sortListByFixedOrderArray(toList("张飞", "关羽", "刘备"), toArray("刘备", "张飞", "关羽")), // contains("刘备", "张飞", "关羽"));
重构: 对于以下代码:
try{ Collections.sort( list, new FixedOrderComparator<>( StoPropertyConstants.PRPT_ITEM_HYPELAUNCH, StoPropertyConstants.PRPT_ITEM_MIADIDAS_VALUE_EN, StoPropertyConstants.PRPT_ITEM_PRESONALLZATION_CODE, StoPropertyConstants.PRPT_ITEM_PERSALES_CODE, StoPropertyConstants.PRPT_ITEM_VIP_CODE, StoPropertyConstants.PRPT_ITEM_COMINGSOON_CODE, StoPropertyConstants.PRPT_ITEM_DISCOUNT_CODE, StoPropertyConstants.PRPT_ITEM_NORMAL_CODE, StoPropertyConstants.PRPT_ITEM_NOSALE)); }catch (Exception e){ LOGGER.error("itemType sort error:{},itemType:{}", e, JsonUtil.format(itemType)); }
可以重构成: com.feilong.core.util.SortUtil.sortListByFixedOrderArray( list, StoPropertyConstants.PRPT_ITEM_HYPELAUNCH, StoPropertyConstants.PRPT_ITEM_MIADIDAS_VALUE_EN, StoPropertyConstants.PRPT_ITEM_PRESONALLZATION_CODE, StoPropertyConstants.PRPT_ITEM_PERSALES_CODE, StoPropertyConstants.PRPT_ITEM_VIP_CODE, StoPropertyConstants.PRPT_ITEM_COMINGSOON_CODE, StoPropertyConstants.PRPT_ITEM_DISCOUNT_CODE, StoPropertyConstants.PRPT_ITEM_NORMAL_CODE, StoPropertyConstants.PRPT_ITEM_NOSALE);
Type Parameters: