private static void LookupWorldCharacter <T>(Dictionary <string, AlphaKeyGroup <T> > table, IEnumerable <T> items, Func <T, string> getKey) { var chars = new CharacterGroupings(); foreach (var c in chars.Where(ch => ch.Label.Length > 0).Select(ch => ch.Label[0].ToString().ToUpper()).Where(c => !table.ContainsKey(c) && c != ".")) { table.Add(c, new AlphaKeyGroup <T>(c)); } foreach (var item in items) { var key = getKey(item); var lookupChar = chars.Lookup(key).ToUpper(); var index = lookupChar.Length > 1 ? (lookupChar.Any(CharEx.IsAlphabet) ? lookupChar.First(CharEx.IsAlphabet).ToString() : lookupChar) : lookupChar; if (table.ContainsKey(index)) { table[index].Add(item); } else { table.Add(index, new AlphaKeyGroup <T>(index) { item }); } } }
/// <summary> /// Groups and sorts into a list of alpha groups based on a string selector. /// </summary> /// <typeparam name="TSource">Type of the items in the list.</typeparam> /// <param name="source">List to be grouped and sorted.</param> /// <param name="selector">A selector that will provide a value that items to be sorted and grouped by.</param> /// <returns>A list of JumpListGroups.</returns> public static List <JumpListGroup <TSource> > ToAlphaGroups <TSource>( this IEnumerable <TSource> source, Func <TSource, string> selector) { // Get the letters representing each group for current language using CharacterGroupings class var characterGroupings = new CharacterGroupings(); // Create dictionary for the letters and replace '...' with proper globe icon var keys = characterGroupings.Where(x => x.Label.Count() >= 1) .Select(x => x.Label) .ToDictionary(x => x); keys["..."] = "\uD83C\uDF10"; // Create groups for each letters var groupDictionary = keys.Select(x => new JumpListGroup <TSource>() { Key = x.Value }) .ToDictionary(x => (string)x.Key); // Sort and group items into the groups based on the value returned by the selector var query = from item in source orderby selector(item) select item; foreach (var item in query) { var sortValue = selector(item); groupDictionary[keys[characterGroupings.Lookup(sortValue)]].Add(item); } return(groupDictionary.Select(x => x.Value).ToList()); }
public static List <JumpListGroup <TSource> > ToAlphaGroups <TSource>( this IEnumerable <TSource> source, Func <TSource, string> selector) { var characterGroupings = new CharacterGroupings(); var keys = characterGroupings.Where(x => x.Label.Count() >= 1) .Select(x => x.Label) .ToDictionary(x => x); keys["..."] = "\uD83C\uDF10"; var groupDictionary = keys.Select(x => new JumpListGroup <TSource>() { Key = x.Value }) .ToDictionary(x => (string)x.Key); var query = from item in source orderby selector(item) select item; foreach (var item in query) { var sortValue = selector(item); groupDictionary[keys[characterGroupings.Lookup(sortValue)]].Add(item); } return(groupDictionary.Select(x => x.Value).ToList()); }
protected void CreateCharacterGroupings() { // Get the letters representing each group for current language using CharacterGroupings class _CharacterGroupings = new CharacterGroupings(); // Create dictionary for the letters and replace '...' with proper globe icon var keys = _CharacterGroupings.Where(x => x.Label.Count() >= 1) .Select(x => x.Label) .ToDictionary(x => x); keys["..."] = "\uD83C\uDF10"; }
public List <MusicGroup> GroupMusicByFirstLetter(IEnumerable <MusicUi> source) { CharacterGroupings cgs = new CharacterGroupings(); var musicGroupList = source.GroupBy(mu => cgs.Lookup(mu.Title).Replace("拼音", "")).Select(g => new MusicGroup(g.Key, g)).ToList(); var cgsGroupList = cgs.Where(c => !c.Label.Contains("拼音") && musicGroupList.All(mg => mg.Name != c.Label)) .Select(c => new MusicGroup(c.Label)).ToList(); musicGroupList.AddRange(cgsGroupList); return(musicGroupList.Where(mg => mg.Items.Any() || !string.IsNullOrWhiteSpace(mg.Name)).ToList()); }
private static Dictionary <string, JumpListGroup <TSource> > ToAlphaGroupsDictionary <TSource>( IEnumerable <TSource> source, Func <TSource, string> selector) { // Get the letters representing each group for current language using CharacterGroupings class var characterGroupings = new CharacterGroupings(); // Create dictionary for the letters and replace '...' with proper globe icon var keys = characterGroupings.Where(x => x.Label.Count() >= 1) .Select(x => x.Label) .ToDictionary(x => x); keys["..."] = "\uD83C\uDF10"; // Create groups for each letters var groupDictionary = keys.Select(x => new JumpListGroup <TSource>() { Key = x.Value }) .ToDictionary(x => (string)x.Key); // Sort and group items into the groups based on the value returned by the selector var query = from item in source orderby selector(item) select item; foreach (var item in query) { try { var sortValue = selector(item); string lookup = characterGroupings.Lookup(sortValue); string key; if (!keys.TryGetValue(lookup, out key)) { key = "\uD83C\uDF10"; } groupDictionary[key].Add(item); } catch (Exception ex) { Debug.WriteLine(ex); } } return(groupDictionary); }
/// <summary> /// Groups and sorts into a list of alpha groups based on a string selector. /// </summary> /// <typeparam name="TSource">Type of the items in the list.</typeparam> /// <param name="source">List to be grouped and sorted.</param> /// <param name="selector">A selector that will provide a value that items to be sorted and grouped by.</param> /// <returns>A list of JumpListGroups.</returns> public static Dictionary <string, List <TSource> > ToAlphaGroups <TSource>( this IEnumerable <TSource> source, Func <TSource, string> selector) { // Get the letters representing each group for current language using CharacterGroupings class var characterGroupings = new CharacterGroupings(); // Create dictionary for the letters and replace '...' with proper globe icon var keys = characterGroupings.Where(x => x.Label.Any()) .Select(x => x.Label) .ToDictionary(x => x); keys["..."] = "\uD83C\uDF10"; // Create groups for each letters var groupDictionary = keys.ToDictionary(x => x.Key, v => new List <TSource>()); // Sort and group items into the groups based on the value returned by the selector var query = from item in source orderby selector(item) select item; foreach (var item in query) { var sortValue = selector(item); var key = characterGroupings.Lookup(sortValue); if (keys.ContainsKey(key)) { groupDictionary[key].Add(item); } else { groupDictionary["..."].Add(item); } } return(groupDictionary); }