From 7e22e5c1e7d980ffa065aeb93f6db9c590cf50a7 Mon Sep 17 00:00:00 2001 From: Elivo Date: Thu, 19 Mar 2026 23:40:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95=20.sort=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E4=B8=AA=E5=AF=B9=E6=AF=94=E5=99=A8?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/CollectionUtility.cs | 71 +++++++++++++++++++++++++++++++------ Apewer/_Extensions.cs | 2 +- 2 files changed, 62 insertions(+), 11 deletions(-) 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);