C#_技巧:字符串分组排序

思想//GroupBy+ToDictionary实现Dictionary<>

            List<string> list = new List<string>();
            //list赋值。略
var query = list.GroupBy(p => { char a=p.ToString().ToLower()[0]; return a >= 'o' ? "O-Z" : a >= 'h' ? "H-N" : "A-G"; }).ToDictionary(p => p.Key, p => p);
//双遍历
foreach (var item in query)
            {
                Console.WriteLine("{0}", item.Key);//IGrouping<TKey,TValue>
                foreach (var subItem in item.Value)
                {
                    Console.WriteLine("  {0}", subItem);
                }
            }

 

posted @ 2016-05-30 14:59  shinchan  阅读(1066)  评论(0编辑  收藏  举报