Feedback

Nach Häufigkeit sortieren

Sprache: C#

Mit dieser Funktion kann man die Elemente in einem IEnumerable nach der Häufigkeit sortieren. source ist die zu sortierende Quelle descending gibt an ob absteigend sortiert werden soll (true = absteigend, false = aufsteigend) comparer gibt an, wie die Elemente verglichen werden sollen. Beispiel: Es wird absteigend mit dem Standardvergleich sortiert List<int> TestZahlen = new List<int>() { 1, 2, 3, 4, 1, 2, 1, 3, 3, 1 }; Ausgabe: 1, 3, 2, 4
public static IEnumerable<T> SortByFrequenzy<T>(IEnumerable<T> source, bool descending = true)
        {
            Dictionary<T, int> counter = new Dictionary<T, int>();
            foreach (T item in source)
            {
                if (counter.ContainsKey(item))
                { continue; }
                else
                { counter.Add(item, source.Count(x => x.Equals(item))); }
            }
            var ordered = descending ? counter.OrderByDescending(x => x.Value) : counter.OrderBy(x => x.Value);
            foreach (var item in ordered)
            {
                yield return item.Key;
            }
        }

        public static IEnumerable<T> SortByFrequenzy<T>(IEnumerable<T> source, IEqualityComparer<T> comparer, bool descending = true)
        {
            Dictionary<T, int> counter = new Dictionary<T, int>(comparer);
            foreach (T item in source)
            {
                if (counter.ContainsKey(item))
                { continue; }
                else
                { counter.Add(item, source.Count(x => x.Equals(item))); }
            }
            var ordered = descending ? counter.OrderByDescending(x => x.Value) : counter.OrderBy(x => x.Value);
            foreach (var item in ordered)
            {
                yield return item.Key;
            }
        }
public static IEnumerable<T> SortByFrequenzy<T>(IEnumerable<T> source, bool descending = true)
        {
            Dictionary<T, int> counter = new Dictionary<T, int>();
            foreach (T item in source)
            {
                if (counter.ContainsKey(item))
                { continue; }
                else
                { counter.Add(item, source.Count(x => x.Equals(item))); }
            }
            var ordered = descending ? counter.OrderByDescending(x => x.Value) : counter.OrderBy(x => x.Value);
            foreach (var item in ordered)
            {
                yield return item.Key;
            }
        }

        public static IEnumerable<T> SortByFrequenzy<T>(IEnumerable<T> source, IEqualityComparer<T> comparer, bool descending = true)
        {
            Dictionary<T, int> counter = new Dictionary<T, int>(comparer);
            foreach (T item in source)
            {
                if (counter.ContainsKey(item))
                { continue; }
                else
                { counter.Add(item, source.Count(x => x.Equals(item))); }
            }
            var ordered = descending ? counter.OrderByDescending(x => x.Value) : counter.OrderBy(x => x.Value);
            foreach (var item in ordered)
            {
                yield return item.Key;
            }
        }