Exemple #1
0
        public static IComparer <T> Create <TKey1, TKey2>(Func <T, TKey1> keySelector1, Func <T, TKey2> keySelector2)
        {
            if (keySelector1 == null)
            {
                throw new ArgumentNullException(nameof(keySelector1));
            }
            if (keySelector2 == null)
            {
                throw new ArgumentNullException(nameof(keySelector2));
            }

            var c1 = Comparer2 <TKey1> .GetDefault();

            var c2 = Comparer2 <TKey2> .GetDefault();

            return(Comparer <T> .Create((x, y) =>
            {
                var d = c1.Compare(keySelector1(x), keySelector1(y));
                if (d != 0)
                {
                    return d;
                }
                return c2.Compare(keySelector2(x), keySelector2(y));
            }));
        }
Exemple #2
0
        public static IComparer <T> Create <TKey>(Func <T, TKey> keySelector, bool descending = false)
        {
            if (keySelector == null)
            {
                throw new ArgumentNullException(nameof(keySelector));
            }

            var c = Comparer2 <TKey> .GetDefault();

            if (descending)
            {
                return(Comparer <T> .Create((x, y) => c.Compare(keySelector(y), keySelector(x))));
            }
            else
            {
                return(Comparer <T> .Create((x, y) => c.Compare(keySelector(x), keySelector(y))));
            }
        }