mirror of https://gitee.com/stylefeng/roses
【8.0】【rule】更新排序工具类
parent
1e7f0fac70
commit
34313ee01f
|
@ -0,0 +1,44 @@
|
||||||
|
package cn.stylefeng.roses.kernel.rule.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.stylefeng.roses.kernel.rule.util.sort.GetSortKey;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 排序工具类
|
||||||
|
* <p>
|
||||||
|
* 一般用来弥补数据库排序功能不足的情况
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @since 2023/8/3 14:43
|
||||||
|
*/
|
||||||
|
public class SortUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对list进行排序,以keys数组传的顺序为准
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @since 2023/8/3 14:44
|
||||||
|
*/
|
||||||
|
public static <T extends GetSortKey> List<T> sortListByObjectKey(List<T> originList, List<?> keys) {
|
||||||
|
|
||||||
|
if (ObjectUtil.isEmpty(originList) || ObjectUtil.isEmpty(keys)) {
|
||||||
|
return originList;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<T> newSortList = new LinkedList<>();
|
||||||
|
for (Object key : keys) {
|
||||||
|
for (T listItem : originList) {
|
||||||
|
Object sortKey = listItem.getSortKey();
|
||||||
|
if (ObjectUtil.equal(key, sortKey)) {
|
||||||
|
newSortList.add(listItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newSortList;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package cn.stylefeng.roses.kernel.rule.util.sort;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取排序的key
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @since 2023/8/3 14:42
|
||||||
|
*/
|
||||||
|
public interface GetSortKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取对象中被排序的Key
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @since 2023/8/3 14:42
|
||||||
|
*/
|
||||||
|
Object getSortKey();
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue