Przeglądaj źródła

Added some new sorting functions

Kenric Nugteren 2 tygodni temu
rodzic
commit
cadf61bf0a
1 zmienionych plików z 36 dodań i 0 usunięć
  1. 36 0
      InABox.Core/CoreUtils.cs

+ 36 - 0
InABox.Core/CoreUtils.cs

@@ -2911,17 +2911,36 @@ namespace InABox.Core
             Array.Sort(list, (a, b) => comparison(a).CompareTo(comparison(b)));
         }
 
+        public static void SortByDescending<T, TProp>(this List<T> list, Func<T, TProp> comparison)
+            where TProp : IComparable
+        {
+            list.Sort((a, b) => comparison(b).CompareTo(comparison(a)));
+        }
+
         public static void SortBy<T, TProp>(this List<T> list, Func<T, TProp> comparison)
             where TProp : IComparable
         {
             list.Sort((a, b) => comparison(a).CompareTo(comparison(b)));
         }
 
+        public static void SortByDescending<T, TProp>(this T[] list, Func<T, TProp> comparison)
+            where TProp : IComparable
+        {
+            Array.Sort(list, (a, b) => comparison(b).CompareTo(comparison(a)));
+        }
+
         public static Comparison<T> OrderBy<T, TProp>(Func<T, TProp> comparison)
             where TProp : IComparable
         {
             return (a, b) => comparison(a).CompareTo(comparison(b));
         }
+
+        public static Comparison<T> OrderByDescending<T, TProp>(Func<T, TProp> comparison)
+            where TProp : IComparable
+        {
+            return (a, b) => comparison(b).CompareTo(comparison(a));
+        }
+
         public static Comparison<T> ThenBy<T>(this Comparison<T> comparison1, Comparison<T> comparison2)
         {
             return (x, y) =>
@@ -2954,6 +2973,23 @@ namespace InABox.Core
             };
         }
 
+        public static Comparison<T> ThenByDescending<T, TProp>(this Comparison<T> comparison1, Func<T, TProp> comparison2)
+            where TProp : IComparable
+        {
+            return (x, y) =>
+            {
+                var result = comparison1(x, y);
+                if (result == 0)
+                {
+                    return comparison2(y).CompareTo(comparison2(x));
+                }
+                else
+                {
+                    return result;
+                }
+            };
+        }
+
         /// <summary>
         /// Compare the elements in this list to the other list, returning <see langword="true"/> if they have all the same
         /// elements, regardless of order.