Example #1
0
        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
                    });
                }
            }
        }
Example #2
0
        /// <summary>
        /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
        /// </summary>
        /// <param name="items">The items to place in the groups.</param>
        /// <param name="ci">The CultureInfo to group and sort by.</param>
        /// <param name="getKey">A delegate to get the key from an item.</param>
        /// <param name="sort">Will sort the data if true.</param>
        /// <returns>An items source for a LongListSelector</returns>
        public static List <AlphaKeyGroup <T> > CreateGroups(IEnumerable <T> items, CultureInfo ci, GetKeyDelegate getKey, bool sort)
        {
            CharacterGroupings        slg  = new CharacterGroupings();
            List <AlphaKeyGroup <T> > list = CreateGroups(slg);

            foreach (T item in items)
            {
                try
                {
                    string index = "";
                    index = slg.Lookup(getKey(item));
                    if (string.IsNullOrEmpty(index) == false)
                    {
                        list.Find(a => a.Key == index).Add(item);
                    }
                }
                catch
                {
                }
            }

            if (sort)
            {
                foreach (AlphaKeyGroup <T> group in list)
                {
                    group.Sort((c0, c1) => { return(ci.CompareInfo.Compare(getKey(c0), getKey(c1))); });
                }
            }

            return(list);
        }
Example #3
0
        /// <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());
        }
Example #4
0
    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());
    }
Example #5
0
        public DataManager()
        {
            AlphaGroups = new ObservableCollection <AlphaKeyContent>();
            CharacterGroupings cg = new CharacterGroupings();

            foreach (var key in cg)
            {
                if (!string.IsNullOrEmpty(key.Label))
                {
                    AlphaGroups.Add(new AlphaKeyContent {
                        Key = key.Label
                    });
                }
            }
            var list = from f in DataList
                       group f by cg.Lookup(f.Name) into g
                       orderby g.Key
                       select g;

            foreach (var group in list)
            {
                var collection = AlphaGroups.FirstOrDefault(f => group.Key.Equals(f.Key));
                if (collection == null)
                {
                    collection = AlphaGroups.Last();
                }
                foreach (var item in group)
                {
                    collection.Add(item);
                }
            }
        }
        public static List <AlphaKeyGroup <T> > CreateGroups(IEnumerable <T> items, Func <T, string> keySelector, bool sort)
        {
            CharacterGroupings        slg  = new CharacterGroupings("EN");
            List <AlphaKeyGroup <T> > list = CreateDefaultGroups(slg);

            foreach (T item in items)
            {
                int index = 0;
                {
                    string label = slg.Lookup(keySelector(item));
                    index = list.FindIndex(alphakeygroup => (alphakeygroup.Key.Equals(label, StringComparison.CurrentCulture)));
                }

                if (index >= 0 && index < list.Count)
                {
                    list[index].InternalList.Add(item);
                }
            }

            if (sort)
            {
                foreach (AlphaKeyGroup <T> group in list)
                {
                    group.InternalList.Sort((c0, c1) => { return(keySelector(c0).CompareTo(keySelector(c0))); });
                }
            }

            return(list);
        }
Example #7
0
        public static List<ListGroup<T>> GroupList<T>(IEnumerable<T> listToGroup, GetNameDelegate<T> getName, bool sort)
        {
            string numberKey = "0 – 9";
            string displayNumberKey = "#";
            List<ListGroup<T>> groupedList = new List<ListGroup<T>>();
            CharacterGroupings slg = new CharacterGroupings();
            foreach (CharacterGrouping key in slg)
            {
                if (string.IsNullOrWhiteSpace(key.Label) == false){
                    string displayKey = key.Label;
                    if (displayKey.Equals(numberKey))
                    {
                        displayKey = displayNumberKey;
                    }
                    groupedList.Add(new ListGroup<T>(displayKey));
                }
            }

#if WINDOWS_APP //Duct tape for Windows 8
            if (groupedList.Find(t => t.Key == "...") == null)
            {
                groupedList.Add(new ListGroup<T>("..."));
            }
#endif //Duct tape ends

            foreach (T item in listToGroup)
            {
                string itemName = GetTrimmedName(getName(item));

                string groupLabel = slg.Lookup(itemName);
                if (groupLabel.Equals(numberKey))
                {
                    groupLabel = displayNumberKey;
                }
                if (!string.IsNullOrEmpty(groupLabel))
                {
                    var groupToAddTo = groupedList.Find(t => t.Key == groupLabel);

#if WINDOWS_APP //Duct tape for Windows 8
                    if (groupToAddTo == null)
                    {
                        groupToAddTo = groupedList.Find(t => t.Key == "...");
                    }
#endif //Duct tape ends

                    groupToAddTo.Add(item);                    
                }
            }

            if (sort)
            {
                foreach (ListGroup<T> group in groupedList)
                {
                    group.Sort((c0, c1) => { return CultureInfo.InvariantCulture.CompareInfo.Compare(GetTrimmedName(getName(c0)), GetTrimmedName(getName(c1))); });
                }
            }

            return groupedList;
        }
Example #8
0
        /// <summary>
        /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
        /// </summary>
        /// <param name="items">The items to place in the groups.</param>
        /// <param name="ci">The CultureInfo to group and sort by.</param>
        /// <param name="getKey">A delegate to get the key from an item.</param>
        /// <param name="sort">Will sort the data if true.</param>
        /// <returns>An items source for a LongListSelector</returns>
        public static IEnumerable <GroupedItem <T> > CreateGroupsByAlpha(IEnumerable <T> items)
        {
            var g      = new CharacterGroupings("zh-CN");
            var groups = from i in items group i by RemovePinYin(g.Lookup(i.Key));

            var ordered = groups.OrderBy(z => z.Key, new StringHelper());

            return(from p in ordered where !p.IsNullorEmpty() select new GroupedItem <T>(p));
        }
Example #9
0
        /// <summary>
        /// Get item index with indexer-specific logic for grouping.
        /// </summary>
        /// <param name="item">The item to be indexed.</param>
        /// <returns>Item's index.</returns>
        /// <seealso cref="GetIndex"/>
        public string GetIndexForGroup(CommonViewItemModel item)
        {
            if (string.IsNullOrEmpty(item?.Title))
            {
                return(CommonSharedStrings.UnknownIndex);
            }

            // Lookup table
            return(_slg.Lookup(item.Title[0].ToString()));
        }
        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());
        }
Example #11
0
        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);
        }
        public static List <AlphaKeyGroup <T> > CreateGroups(IEnumerable <T> items, Func <T, string> keySelector)
        {
            CharacterGroupings        slg  = new CharacterGroupings();
            List <AlphaKeyGroup <T> > list = CreateAZGroups(); //CreateDefaultGroups(slg);

            foreach (T item in items)
            {
                int    index = 0;
                string label = slg.Lookup(keySelector(item));
                index = list.FindIndex(alphagroupkey => (alphagroupkey.Key.Equals(label, StringComparison.CurrentCulture)));
                if (index > -1 && index < list.Count)
                {
                    list[index].Add(item);
                }
            }
            return(list);
        }
Example #13
0
        /// <summary>
        ///     Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
        /// </summary>
        /// <param name="items">The items to place in the groups.</param>
        /// <param name="ci">The CultureInfo to group and sort by.</param>
        /// <param name="getKey">A delegate to get the key from an item.</param>
        /// <param name="sort">Will sort the data if true.</param>
        /// <returns>An items source for a LongListSelector</returns>
        public static OptimizedObservableCollection <AlphaKeyGroup <T> > CreateGroups(IEnumerable <T> items, CultureInfo ci,
                                                                                      GetKeyDelegate getKey,
                                                                                      bool sort)
        {
            var slg  = new CharacterGroupings();
            var list = CreateGroups(slg);

            foreach (T item in items)
            {
                var lookUp = getKey(item);

                if (string.IsNullOrEmpty(lookUp))
                {
                    continue;
                }

                var index  = slg.Lookup(lookUp);
                var indexs = index.ToUpper();

                try
                {
                    list.Find(a => a.Key.Equals(indexs, StringComparison.CurrentCultureIgnoreCase)).Items.Add(item);
                }
                catch
                {
                    list.FirstOrDefault().Items.Add(item);
                }


                if (sort)
                {
                    foreach (AlphaKeyGroup <T> group in list)
                    {
                        group.OrderKey = getKey;
                        var asList = group.ToList();
                        asList.Sort((x, y) => string.Compare(getKey(x), getKey(y), StringComparison.Ordinal));
                        group.SwitchTo(asList);
                    }
                }
            }

            return(new OptimizedObservableCollection <AlphaKeyGroup <T> >(list));
        }
Example #14
0
        public static ObservableCollection <GroupedOC <T> > CreateGrouped <T>(IEnumerable <T> InitialItemsList, Func <T, string> selector)
        {
            ObservableCollection <GroupedOC <T> > GroupedItems = new ObservableCollection <GroupedOC <T> >();
            //ObservableCollection<IGrouping<string, T>> GroupedItems2 = new ObservableCollection<IGrouping<string, T>>();
            //ObservableCollection<GroupedOC<T>> GroupedItems2 = new ObservableCollection<GroupedOC<T>>;

            var characterGroupings = new CharacterGroupings();

            //System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
            //st.Start();
            //foreach (var q in InitialItemsList.GroupBy(s => characterGroupings.Lookup(selector(s))).Select(s => s).OrderBy(t => t.Key))
            //{
            //    GroupedItems2.Add(q);
            //}
            //foreach (var c in characterGroupings)
            //{
            //    bool found = false;
            //    foreach(var g in GroupedItems2)
            //    {
            //        if (g.Key == c.Label) found = true;
            //    }
            //    if (!found)
            //    {
            //        GroupedItems2.Add(new GroupedOC<T>(c.Label));
            //    }
            //}
            //st.Stop();
            //st.Restart();
            foreach (var c in characterGroupings)
            {
                GroupedItems.Add(new GroupedOC <T>(c.Label));
            }
            foreach (var item in InitialItemsList)
            {
                string a = characterGroupings.Lookup(selector(item));
                GroupedItems.FirstOrDefault(e => e.Key.Equals(a)).Add(item);
            }
            //st.Stop();
            return(GroupedItems);
        }
        /// <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);
        }
        public void AssignGroup_Click(object sender, RoutedEventArgs e)
        {
            rootPage.NotifyUser("", NotifyType.StatusMessage);
            if (ActiveGroupings == null)
            {
                rootPage.NotifyUser("Select a language first.", NotifyType.ErrorMessage);
                return;
            }
            if (String.IsNullOrEmpty(CandidateText.Text))
            {
                rootPage.NotifyUser("Enter text to assign to a group.", NotifyType.ErrorMessage);
                return;
            }
            string label = ActiveGroupings.Lookup(CandidateText.Text);

            if (!groups.TryGetValue(label, out UIElement element))
            {
                label   = GlobeString;
                element = groups[GlobeString];
            }
            GroupingsGrid.SelectedItem = element;
            GroupingResult.Text        = $"Group \"{label}\"";
            element.StartBringIntoView();
        }
Example #17
0
        private void Load()
        {
            if (records == null)
            {
                records = TmpData.PasswordKeeper.Records;
            }
            CharacterGroupings groupings = new CharacterGroupings();

            observablerecords = new RangedObservableCollection <PasswordRecordViewModelGroup>(records.Select(a => PasswordRecordViewModel.CreateFromRecord(a))
                                                                                              .OrderBy(a => a.DisplayName).GroupBy((a) => groupings.Lookup(a.DisplayName))
                                                                                              .Select(a => new PasswordRecordViewModelGroup(a.Key, ct(a))));

            UpdateForVisualState(AdaptiveStates.CurrentState);

            // Don't play a content transition for first item load.
            // Sometimes, this content will be animated as part of the page transition.
            DisableContentTransitions();
        }
Example #18
0
        /// <summary>
        ///     Create a list of AlphaGroup with keys set by a SortedLocaleGrouping.
        /// </summary>
        /// <param name="items">The items to place in the groups.</param>
        /// <param name="ci">The CultureInfo to group and sort by.</param>
        /// <param name="getKey">A delegate to get the key from an item.</param>
        /// <param name="sort">Will sort the data if true.</param>
        /// <returns>An items source for a LongListSelector</returns>
        public static OptimizedObservableCollection<AlphaKeyGroup> CreateGroups(IEnumerable<object> items, CultureInfo ci,
            GetKeyDelegate getKey,
            bool sort = true)
        {
            var slg = new CharacterGroupings();
            var list = CreateGroups(slg);

            foreach (var item in items)
            {
                var lookUp = getKey(item);

                if (string.IsNullOrEmpty(lookUp))
                    continue;

                var index = slg.Lookup(lookUp);
                if (string.IsNullOrEmpty(index) == false)
                {
                    list.Find(a => a.Key.EqualsIgnoreCase(index)).Items.Add(item);
                }
            }

            var max = list.Select(p => p.Count).Max();

            if (sort)
            {
                foreach (var group in list)
                {
                    group.OrderKey = getKey;
                    var percent = group.Count > 1 ? Math.Log(group.Count, max) : Math.Log(2, max) / 2;
                    percent = Math.Max(.1, percent);
                    if (double.IsNaN(percent))
                        percent = 0.1;
                    if (group.Count == 0)
                        percent = 0;

                    group.GridLeftLength = new GridLength(percent, GridUnitType.Star);
                    group.GridRightLength = new GridLength(1 - percent, GridUnitType.Star);

                    var asList = group.ToList();
                    asList.Sort((x, y) => string.Compare(getKey(x), getKey(y), StringComparison.Ordinal));
                    group.SwitchTo(asList);

                    group.CollectionChanged += (sender, args) =>
                    {
                        max = list.Select(p => p.Count).Max();
                        foreach (var other in list)
                        {
                            percent = other.Count > 1 ? Math.Log(other.Count, max) : Math.Log(2, max) / 2;
                            percent = Math.Max(.1, percent);
                            if (double.IsNaN(percent))
                                percent = 0.1;
                            if (other.Count == 0)
                                percent = 0;

                            other.GridLeftLength = new GridLength(percent, GridUnitType.Star);
                            other.GridRightLength = new GridLength(1 - percent, GridUnitType.Star);
                        }
                    };
                }
            }

            return new OptimizedObservableCollection<AlphaKeyGroup>(list);
        }