類似通訊錄的A-Z字母分組實現
阿新 • • 發佈:2019-01-07
直接貼程式碼:
package com.bsd.join.utils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
/**
* 按拼音首字母表排序
* 例如:首字母A開頭的放在A下面,B開頭的放到B下面
* @author jzz
*
*/
@Slf4j
public class SortByLetterAscUtils {
/**
*
* @param object 一個集合
* @param clazz 集合存放的類
* @param field 要排序的類的欄位,欄位的getXyz() X要大寫
* @return
* @throws Exception
*/
public static Map<String,Object> sortByLetterAsc(Object object,Class clazz,String field) throws Exception {
if(object instanceof List){
List<Object> list = (List<Object>) object;
Class<?> c = Class.forName(clazz.getName());
Object o = c.newInstance();
Map<String,Object> map = new HashMap<String,Object>();
//按拼音首字母表排序
Map<String,ArrayList<Object>> letterMap = new TreeMap<String,ArrayList<Object>>(new MapSortUtil().getMapKeyComparator());
if(!list.isEmpty()) {
for (Object t : list) {
o = t;
String pinYinFirstLetter = getFieldValue(field, o);
if(!letterMap.containsKey(pinYinFirstLetter) && pinYinFirstLetter.matches("[A-Z]")){
letterMap.put(pinYinFirstLetter, new ArrayList<Object>());
}
}
Iterator<Entry<String, ArrayList<Object>>> entries = letterMap.entrySet().iterator();
while(entries.hasNext()){
Entry<String, ArrayList<Object>> next = entries.next();
ArrayList<Object> listTemp = new ArrayList<Object>();
for (Object t : list) {
o = t;
String pinYinFirstLetter = getFieldValue(field, o);
if(!StringUtils.isEmpty(pinYinFirstLetter) && next.getKey().equals(pinYinFirstLetter)){
listTemp.add(t);
}
}
next.getValue().addAll(listTemp);
}
log.debug("對letterMap按照升序排序,#放到最後");
}
ArrayList<Object> listTemp2 = new ArrayList<Object>();
if(!list.isEmpty()){
for (Object t : list) {
o = t;
String pinYinFirstLetter = getFieldValue(field, o);
if (!pinYinFirstLetter.matches("[A-Z]")) {
listTemp2.add(t);
}
}
if(!listTemp2.isEmpty()){
letterMap.put("#", listTemp2);
}
}
//保證map排序後的順序不
JSONObject jsonObject = new JSONObject(true);
jsonObject.put("key", letterMap);
map.put("data",jsonObject);
return map;
}else {
log.info("轉化為list失敗");
return null;
}
}
/**
* 獲取傳遞欄位的屬性值
* @param field
* @param o
* @return
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static String getFieldValue(String field, Object o)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
String name = field.substring(0, 1).toUpperCase() + field.substring(1);
Method method = o.getClass().getMethod("get"+name);
String value = (String) method.invoke(o);
String pinYinFirstLetter = Cn2Spell.getPinYinFirstLetter(value).toUpperCase();
return pinYinFirstLetter;
}
}
使用demo:
GsGoodsCategoryExample goodsCategoryExample = new GsGoodsCategoryExample();
goodsCategoryExample.createCriteria().andStatusEqualTo(1).andCategorySourceEqualTo(9).andCategoryIdIn(categoryIdsList);
List<GsGoodsCategory> gsGoodsCategoryList = gsGoodsCategoryMapper.selectByExample(goodsCategoryExample);
Map<String, Object> sortByLetterAsc = SortByLetterAscUtils.sortByLetterAsc(gsGoodsCategoryList, GsGoodsCategory.class,"categoryName");
System.out.println("sortByLetterAsc:"+sortByLetterAsc.get("data"));
記得替換自己的List集合,和對那個model類的屬性排序
用到的請拿手!