字母

首字母

FirstLetter

package sidebar;

import value.Magic;

/**
 * @decs: 首字母
 * @date: 2018/6/23 11:04
 * @version: v 1.0
 */
public class FirstLetter {
    private static int END = 63486;
    /**
     * 按声母表示,该表是GB2312码出现头个汉字(【啊】为首字母头个汉字)
     * i、u、v不做声母(自定规则随前字母)
     */
    private static char[] charTable = {'啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈', '击', '喀', '垃',
            '妈', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌', '塌', '挖', '昔', '压', '匝',};
    /**
     * 二十六字母区间对应二十七端点
     * GB2312码汉字区间十进制
     */
    private static int[] table = new int[27];
    /**
     * 对应首字母区间表
     */
    private static char[] initialTable = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'h', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 't', 't', 'w', 'x', 'y', 'z',};

    // 初始化
    static {
        for (int i = 0; i < Magic.INT_EL; i++) {
            // GB2312码首字母区间端点表(十进制)
            table[i] = gbValue(charTable[i]);
        }
        // 区间表结尾
        table[26] = END;
    }

    private FirstLetter() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 据一含汉字字符串返一汉字拼音首字母字符串(字符依次读入、判断、输出)
     */
    public static String getFirstLetter(String sourceStr) {
        String str = sourceStr.toUpperCase();
        char ch = char2initial(str.charAt(0));
        if (isLetter(ch)) {
            return String.valueOf(ch).toUpperCase();
        }
        return "#";
    }

    /**
     * 字母否
     */
    private static boolean isLetter(char ch) {
        return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
    }

    /**
     * 指定串首字母
     */
    public static String getAllFirstLetter(String sourceStr) {
        StringBuilder result = new StringBuilder();
        String str = sourceStr.toUpperCase();
        int strLength = str.length();
        int i;
        try {
            for (i = 0; i < strLength; i++) {
                result.append(char2initial(str.charAt(i)));
            }
        } catch (Exception e) {
            result = new StringBuilder();
        }
        return result.toString();
    }

    /**
     * 输字符获声母
     * 英文字母返对应大写字母
     * 其它非简体汉字返0
     */
    private static char char2initial(char ch) {
        // 英文字母处理(小写转大写,大写直返)
        if (ch >= Magic.CHAR_A_SMALL && ch <= Magic.CHAR_Z_SMALL) {
            return ch;
        }
        if (ch >= Magic.CHAR_A_BIG && ch <= Magic.CHAR_Z_BIG) {
            return ch;
        }
        // 非英文字母处理,转首字母后判码表内否(内直返,非内于码表内判)
        // 汉字转首字母
        int gb = gbValue(ch);
        int begin = 45217;
        // 码表区间前直返
        if ((gb < begin) || (gb > END)) {
            return ch;
        }
        int i;
        for (i = 0; i < Magic.INT_EL; i++) {
            // 判匹配码表区间(形如“[,)”),匹配则break
            if ((gb >= table[i]) && (gb < table[i + 1])) {
                break;
            }
        }
        if (gb == END) {
            // 补GB2312区间最右端
            i = 25;
        }
        // 码表区间中返首字母
        return initialTable[i];
    }

    /**
     * 取汉字编码(cn 汉字)
     */
    private static int gbValue(char ch) {
        // 汉字(GB2312码)转十进制
        String str = "";
        str += ch;
        try {
            byte[] bytes = str.getBytes("GB2312");
            if (bytes.length < Magic.INT_ER) {
                return 0;
            }
            return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);
        } catch (Exception e) {
            return 0;
        }
    }
}

实体类

public static class DataBean {
    private String index;   
    private String jabName;

    public String getIndex() {
        return index;

    public void setIndex(String name) {
        this.index = FirstLetter.getFirstLetter(name);
    }

    public String getJabName() {
        return jabName;
    }

    public void setJabName(String jabName) {
        this.jabName = jabName;
     }        
}

主代码

dataBean.setIndex(dataBean.getJabName());

首字母排序

AcronymSorting

package sidebar;

import java.util.Comparator;

import bean.Book;

/**
 * @decs: 首字母排序
 * @date: 2018/6/23 11:00
 * @version: v 1.0
 */
public class AcronymSorting implements Comparator<Book.DataBean> {
    @Override
    public int compare(Book.DataBean dataBean, Book.DataBean db) {
        if (dataBean == null || db == null) {
            return 0;
        }
        String lhsSortLetters = dataBean.getIndex().substring(0, 1).toUpperCase();
        String rhsSortLetters = db.getIndex().substring(0, 1).toUpperCase();
        return lhsSortLetters.compareTo(rhsSortLetters);
    }
}

主代码

allList = acronymSorting(allList);

private List<Book.DataBean> acronymSorting(List<Book.DataBean> originalList) {
    if (originalList.isEmpty()) {
        return originalList;
    } else {
        List<Book.DataBean> sortList = new ArrayList<>();
        for (Book.DataBean dataBean : originalList) {
            // 首字母
            dataBean.setIndex(dataBean.getJabName());
            sortList.add(dataBean);
        }
         / 首字母排序
           Collections.sort(sortList, new AcronymSorting());
         // 清初始数据以存排序数据(避重复)
        originalList.clear();
        return sortList;
    }
}

猜你喜欢

转载自blog.csdn.net/zsp_android_com/article/details/80783888