|
|
|
@ -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 排序
|
|
|
|
|
|
|
|
/// <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; |
|
|
|
} |
|
|
|
|
|
|
|
/// <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; |
|
|
|
} |
|
|
|
|
|
|
|
|