如何使用C#進行中文筆畫分組

在公司有人問到如何做中文字的筆畫分群,一時間也想不起來.NET是否已經提供此功能,後來想了一下,因為,NET在繁體中文的排序預設是用筆畫,所以原則上只要知道個筆畫排序第一的字就可以在排序後進行分組。

我試著寫了一個C#的Extension Method,筆畫排序第一個字取自我女兒的國語字典,目前僅適用於繁體中文的筆畫分組,初步測試可行。

public static Dictionary<string, List<string>> GroupBy(this List<string> wordList) {
            List<string> groupWords = new List<string>(new string[] {
"一", "丁", "三", "丑", "丙", "丞", "串", "並", "亟", "乘", "乾", "傒",
"亂", "僧", "儆", "儒", "優", "叢", "嚥", "勸", "亹", "儼", "囌", "囑",
"囔", "灤", "灨", "戇", "爨", "鱺", "灩", "籲", "鱻", "黌" });
 
            Dictionary<string, List<string>> wordGroups = new Dictionary<string, List<string>>();
            wordGroups.Add("一", new List<string>());
            wordGroups.Add("二", new List<string>());
            wordGroups.Add("三", new List<string>());
            wordGroups.Add("四", new List<string>());
            wordGroups.Add("五", new List<string>());
            wordGroups.Add("六", new List<string>());
            wordGroups.Add("七", new List<string>());
            wordGroups.Add("八", new List<string>());
            wordGroups.Add("九", new List<string>());
            wordGroups.Add("十", new List<string>());
            wordGroups.Add("十一", new List<string>());
            wordGroups.Add("十二", new List<string>());
            wordGroups.Add("十三", new List<string>());
            wordGroups.Add("十四", new List<string>());
            wordGroups.Add("十五", new List<string>());
            wordGroups.Add("十六", new List<string>());
            wordGroups.Add("十七", new List<string>());
            wordGroups.Add("十八", new List<string>());
            wordGroups.Add("十九", new List<string>());
            wordGroups.Add("二十", new List<string>());
            wordGroups.Add("二十一", new List<string>());
            wordGroups.Add("二十二", new List<string>());
            wordGroups.Add("二十三", new List<string>());
            wordGroups.Add("二十四", new List<string>());
            wordGroups.Add("二十五", new List<string>());
            wordGroups.Add("二十六", new List<string>());
            wordGroups.Add("二十七", new List<string>());
            wordGroups.Add("二十八", new List<string>());
            wordGroups.Add("二十九", new List<string>());
            wordGroups.Add("三十", new List<string>());
            wordGroups.Add("三十一", new List<string>());
            wordGroups.Add("三十二", new List<string>());
            wordGroups.Add("三十三", new List<string>());
            wordGroups.Add("三十五", new List<string>());
 
            List<string> wordGroupKeys = new List<string>(wordGroups.Keys);
            List<string> tempList = new List<string>(wordList);
            tempList.AddRange(groupWords);
            tempList.Sort();
            int n = -1;
            int keyIndex = -1;
            foreach (string word in tempList) {
                n = groupWords.IndexOf(word);
                if (n >= 0) {
                    if (keyIndex >= 0 && groupWords[keyIndex] == word) {
                        wordGroups[wordGroupKeys[n]].Add(word);
                    }
                    keyIndex = n;
                } else {
                    wordGroups[wordGroupKeys[keyIndex]].Add(word);
                }
            }
            return wordGroups;
        }
    }

呼叫方式:

List<string> wordList = new List<string>(new string[] { "功", "能", "測", "試" });
Dictionary<string, List<string>> wordGroups = wordList.GroupBy();

留言

熱門文章