`
cevin15
  • 浏览: 25924 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

对内容列表按英文字母进行排序

阅读更多
实现对内容列表,比如说种类,地方名按英文字母进行排序。效果如下

引用
D
--都是
E
--耳朵
C
--策略
--测定
L
--萝莉
K
--靠谱
--看来
P
--破洞


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
 * 字母排序类
 * @author Cevin
 * @date 2012-1-12 下午04:08:18
 */
public class LetterSort<T> {
	
	/**
	 * 根据首字母对list进行排序
	 */
	public Map<Character, List<T>> sortByLetter(List<T> list2Sort, LetterSortHelp<T> help){
		PinyinUtils pinyin = new PinyinUtils();
		Map<Character, List<T>> map = initSortMap();
		
		for(T tmp : list2Sort){
			String letters = pinyin.string2Alpha(help.getStr4Sort(tmp));
			if(letters.length()==0){
				continue;
			}
			List<T> list = map.get(letters.charAt(0));
			if(list!=null){
				list.add(tmp);
			}
		}
		
		return map;
	}
	
	/**
	 * 初始化字母key的map集合
	 */
	private Map<Character, List<T>> initSortMap(){
		Map<Character, List<T>> map = new TreeMap<Character, List<T>>();
		char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
		for(char c : letters){
			map.put(c, new ArrayList<T>());
		}		
		return map;
	}
	
	public static void main(String[] args){
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, "策略", "靠谱", "测定","都是","耳朵","看来","破洞","萝莉");
		LetterSort<String> ls = new LetterSort<String>();
		Map<Character, List<String>> map = ls.sortByLetter(list, new LetterSortHelp<String>() {
			@Override
			public String getStr4Sort(String t) {
				return t;
			}
		});
		for(Entry<Character, List<String>> entry : map.entrySet()){
			if(entry.getValue().size()!=0){
				System.out.println(entry.getKey());
				for(String str : entry.getValue()){
					System.out.println("--" + str);
				}
			}
		}
	}
}


留了下面这个接口,可以方便的对需要排序的不同列表类型进行排序。
/**
 * 字母排序协助类
 * @author Cevin
 * @date 2012-1-12 下午04:25:43
 */
public interface LetterSortHelp<T> {
	
	/**
	 * 获取T中要进行排序的字符串
	 */
	public String getStr4Sort(T t);
}


这个拼音工具类不是自己原创的...
/**
 * 拼音工具类
 * @author Cevin
 * @date 2012-1-12 下午04:27:09
 */
public class PinyinUtils {

	// 字母Z使用了两个标签,这里有27个值,i, u, v都不做声母, 跟随前面的字母
	private char[] chartable = { '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈',
			'击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌', '塌',
			'挖', '昔', '压', '匝', '座' };

	private char[] alphatable = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
			'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
			'W', 'X', 'Y', 'Z' };

	private int[] table = new int[27];

	// 初始化
	{
		for (int i = 0; i < 27; ++i) {
			table[i] = gbValue(chartable[i]);
		}
	}

	/**
	 *根据字符得到它的声母, 英文字母返回对应的大写字母 其他非简体汉字返回 '0'
	 */
	public char char2Alpha(char ch) {

		if (ch >= 'a' && ch <= 'z')
			return (char) (ch - 'a' + 'A');
		if (ch >= 'A' && ch <= 'Z')
			return ch;

		int gb = gbValue(ch);
		if (gb < table[0])
			return '0';

		int i;
		for (i = 0; i < 26; ++i) {
			if (match(i, gb))
				break;
		}

		if (i >= 26)
			return '0';
		else
			return alphatable[i];
	}

	/**
	 * 根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
	 */
	public String string2Alpha(String sourceStr) {
		String Result = "";
		int StrLength = sourceStr.length();
		int i;
		try {
			for (i = 0; i < StrLength; i++) {
				Result += char2Alpha(sourceStr.charAt(i));
			}
		} catch (Exception e) {
			Result = "";
		}
		return Result;
	}

	private boolean match(int i, int gb) {
		if (gb < table[i])
			return false;

		int j = i + 1;

		// 字母Z使用了两个标签
		while (j < 26 && (table[j] == table[i]))
			++j;

		if (j == 26)
			return gb <= table[j];
		else
			return gb < table[j];

	}

	private int gbValue(char ch) {
		return gbValue(ch, "gbk");
	}

	// 取出汉字的编码
	private int gbValue(char ch, String encode) {
		String str = new String();
		str += ch;
		try {
			byte[] bytes = str.getBytes(encode);
			if (bytes.length < 2)
				return 0;
			return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);
		} catch (Exception e) {
			return 0;
		}
	}

	public static void main(String[] args) {
		PinyinUtils stringUtils = new PinyinUtils();
		String alpha = stringUtils.string2Alpha("年底了,大家新年快乐");
		System.out.println("alpha = " + alpha);
	}
}


项目需要,简单写了这个工具类。如果大家看了觉得有什么问题都欢迎提出来讨论。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics