Browse Source

扩展方法 .sort 支持多个对比器参数。

master
Elivo 1 month ago
parent
commit
7e22e5c1e7
  1. 71
      Apewer/CollectionUtility.cs
  2. 2
      Apewer/_Extensions.cs

71
Apewer/CollectionUtility.cs

@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json.Linq;
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
@ -474,21 +475,71 @@ namespace Apewer
#region 排序 #region 排序
/// <summary>对列表中的元素排序。</summary> /// <summary>对列表中的元素排序。</summary>
public static List<T> Sort<T>(List<T> list, Func<T, T, int> comparison)
/// <returns>返回排序的数组,用于链式写法。</returns>
/// <exception cref="ArgumentNullException"></exception>
public static List<T> Sort<T>(this List<T> list, params Comparison<T>[] comparisons)
{ {
if (list == null) return null;
if (comparison == null) return list;
list.Sort(new Comparison<T>(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<T>.Default.Compare(a, b);
});
return list; return list;
} }
/// <summary>对数组排序。</summary> /// <summary>对数组排序。</summary>
/// <exception cref="NullReferenceException"></exception>
public static T[] Sort<T>(T[] array, Func<T, T, int> comparison)
/// <returns>返回排序的数组,用于链式写法。</returns>
/// <exception cref="ArgumentNullException" />
/// <exception cref="InvalidOperationException" />
public static T[] Sort<T>(this T[] array)
{ {
if (array == null) return array;
if (comparison == null) return array;
System.Array.Sort(array, new Comparison<T>(comparison));
System.Array.Sort(array);
return array;
}
/// <summary>对数组排序。</summary>
/// <returns>返回排序的数组,用于链式写法。</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException" />
public static T[] Sort<T>(this T[] array, params Comparison<T>[] 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<T>.Default.Compare(a, b);
});
return array; return array;
} }

2
Apewer/_Extensions.cs

@ -475,7 +475,7 @@ public static class Extensions
public static T[] Array<T>(IEnumerable<T> @this, bool excludeNull = false) => CollectionUtility.Array<T>(@this, excludeNull); public static T[] Array<T>(IEnumerable<T> @this, bool excludeNull = false) => CollectionUtility.Array<T>(@this, excludeNull);
/// <summary>对列表中的元素排序。</summary> /// <summary>对列表中的元素排序。</summary>
public static List<T> Sort<T>(this List<T> @this, Func<T, T, int> comparison) => CollectionUtility.Sort(@this, comparison);
public static List<T> Sort<T>(this List<T> @this, Comparison<T> comparison) => CollectionUtility.Sort(@this, comparison);
/// <summary>对字典中的键排序。</summary> /// <summary>对字典中的键排序。</summary>
public static Dictionary<TKey, TValue> SortKey<TKey, TValue>(this Dictionary<TKey, TValue> @this, Func<TKey, TKey, int> comparison) => CollectionUtility.SortKey(@this, comparison); public static Dictionary<TKey, TValue> SortKey<TKey, TValue>(this Dictionary<TKey, TValue> @this, Func<TKey, TKey, int> comparison) => CollectionUtility.SortKey(@this, comparison);

Loading…
Cancel
Save