ifeilong / feilong-core

:gem: Reduce development, Release ideas
Apache License 2.0
529 stars 155 forks source link

sort util 封装数组或者集合 固定的排序方法 #788

Closed venusdrogon closed 5 years ago

venusdrogon commented 5 years ago

image

venusdrogon commented 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"
    ]
venusdrogon commented 5 years ago

对 集合 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:

the generic type Parameters: list the list fixedOrderItems the fixed order items Returns: 如果 list 是null,返回 Collections.emptyList() 如果 list 是empty,返回 list 如果 fixedOrderItems 是null或者是 empty,返回 list Since: 1.14.3 See Also: java.util.Collections.sort(List)