类似通讯录的A-Z字母分组实现

直接贴代码:

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 sortByLetterAsc(Object object,Class clazz,String field) throws Exception {
        if(object instanceof List){
            List list = (List) object;
            Class c = Class.forName(clazz.getName());
            Object o = c.newInstance();
            Map map = new HashMap();

            //按拼音首字母表排序  
            Map> letterMap = new TreeMap>(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());
                    }
                }

                Iterator>> entries  = letterMap.entrySet().iterator();
                while(entries.hasNext()){
                    Entry> next = entries.next();
                    ArrayList listTemp = new ArrayList();
                    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 listTemp2 = new ArrayList();
            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 gsGoodsCategoryList = gsGoodsCategoryMapper.selectByExample(goodsCategoryExample);
        Map sortByLetterAsc = SortByLetterAscUtils.sortByLetterAsc(gsGoodsCategoryList, GsGoodsCategory.class,"categoryName");
        System.out.println("sortByLetterAsc:"+sortByLetterAsc.get("data"));

记得替换自己的List集合,和对那个model类的属性排序
用到的请拿手!

你可能感兴趣的:(java)