diff --git a/Apewer/CollectionUtility.cs b/Apewer/CollectionUtility.cs
index 16ee9b4..b074722 100644
--- a/Apewer/CollectionUtility.cs
+++ b/Apewer/CollectionUtility.cs
@@ -1,4 +1,5 @@
-using System;
+using Newtonsoft.Json.Linq;
+using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
@@ -474,21 +475,71 @@ namespace Apewer
#region 排序
/// 对列表中的元素排序。
- public static List Sort(List list, Func comparison)
+ /// 返回排序的数组,用于链式写法。
+ ///
+ public static List Sort(this List list, params Comparison[] comparisons)
{
- if (list == null) return null;
- if (comparison == null) return list;
- list.Sort(new Comparison(comparison));
+ if (list == null) throw new ArgumentNullException(nameof(list));
+ if (comparisons == null) throw new ArgumentNullException(nameof(comparisons));
+
+ var count = comparisons.Length;
+ if (count < 1) return list;
+ for (var i = 0; i < count; i++)
+ {
+ if (comparisons[i] == null) throw new ArgumentNullException(nameof(comparisons), $"Comparisons[{i}] is null.");
+ }
+
+ list.Sort((a, b) =>
+ {
+ for (var i = 0; i < count; i++)
+ {
+ var compare = comparisons[i].Invoke(a, b);
+ if (compare != 0) return compare;
+ }
+
+ return Comparer.Default.Compare(a, b);
+ });
+
return list;
}
/// 对数组排序。
- ///
- public static T[] Sort(T[] array, Func comparison)
+ /// 返回排序的数组,用于链式写法。
+ ///
+ ///
+ public static T[] Sort(this T[] array)
{
- if (array == null) return array;
- if (comparison == null) return array;
- System.Array.Sort(array, new Comparison(comparison));
+ System.Array.Sort(array);
+ return array;
+ }
+
+ /// 对数组排序。
+ /// 返回排序的数组,用于链式写法。
+ ///
+ ///
+ public static T[] Sort(this T[] array, params Comparison[] comparisons)
+ {
+ if (array == null) throw new ArgumentNullException(nameof(array));
+ if (comparisons == null) throw new ArgumentNullException(nameof(comparisons));
+
+ var count = comparisons.Length;
+ if (count < 1) return array;
+ for (var i = 0; i < count; i++)
+ {
+ if (comparisons[i] == null) throw new ArgumentNullException(nameof(comparisons), $"Comparisons[{i}] is null.");
+ }
+
+ System.Array.Sort(array, (a, b) =>
+ {
+ for (var i = 0; i < count; i++)
+ {
+ var compare = comparisons[i].Invoke(a, b);
+ if (compare != 0) return compare;
+ }
+
+ return Comparer.Default.Compare(a, b);
+ });
+
return array;
}
diff --git a/Apewer/_Extensions.cs b/Apewer/_Extensions.cs
index 7287871..249f9d5 100644
--- a/Apewer/_Extensions.cs
+++ b/Apewer/_Extensions.cs
@@ -475,7 +475,7 @@ public static class Extensions
public static T[] Array(IEnumerable @this, bool excludeNull = false) => CollectionUtility.Array(@this, excludeNull);
/// 对列表中的元素排序。
- public static List Sort(this List @this, Func comparison) => CollectionUtility.Sort(@this, comparison);
+ public static List Sort(this List @this, Comparison comparison) => CollectionUtility.Sort(@this, comparison);
/// 对字典中的键排序。
public static Dictionary SortKey(this Dictionary @this, Func comparison) => CollectionUtility.SortKey(@this, comparison);