|
|
using Apewer.Internals; using System; using System.Collections.Generic; using System.Text;
namespace Apewer {
/// <summary>排序实用工具。</summary>
public class SortUtility {
#region compare
/// <exception cref="ArgumentException"></exception>
public static int Ascend<T>(T a, T b) where T : IComparable { if (a == null) return b == null ? 0 : b.CompareTo(a); return b == null ? 1 : a.CompareTo(b); }
/// <exception cref="ArgumentException"></exception>
public static int Descend<T>(T a, T b) where T : IComparable { return 0 - Ascend(a, b); }
/// <exception cref="ArgumentException"></exception>
public static int AscendT<T>(T a, T b) where T : IComparable<T> { if (a == null) return b == null ? 0 : b.CompareTo(a); return b == null ? 1 : a.CompareTo(b); }
/// <exception cref="ArgumentException"></exception>
public static int DescendT<T>(T a, T b) where T : IComparable<T> { return 0 - AscendT(a, b); }
#endregion
#region enumerable
/// <summary>升序排序。</summary>
public static List<T> Ascend<T>(IEnumerable<T> items) where T : IComparable { var list = new List<T>(items); list.Sort(Ascend); return list; }
/// <summary>降序排序。</summary>
public static List<T> Descend<T>(IEnumerable<T> items) where T : IComparable { var list = new List<T>(items); list.Sort(Descend); return list; }
/// <summary>升序排序。</summary>
public static List<T> AscendT<T>(IEnumerable<T> items) where T : IComparable<T> { var list = new List<T>(items); list.Sort(AscendT); return list; }
/// <summary>降序排序。</summary>
public static List<T> DescendT<T>(IEnumerable<T> items) where T : IComparable<T> { var list = new List<T>(items); list.Sort(DescendT); return list; }
#endregion
#region dictionary
/// <summary>对 Key 排序。</summary>
public static List<KeyValuePair<TKey, TValue>> Keys<TKey, TValue>(Dictionary<TKey, TValue> target, Comparison<TKey> comparison) where TKey : IComparable<TKey> { if (target == null) return null;
var keys = new List<TKey>(); keys.AddRange(target.Keys); if (comparison == null) keys.Sort(); else keys.Sort(comparison);
var output = new List<KeyValuePair<TKey, TValue>>(); foreach (var key in keys) output.Add(key, target[key]);
return output; }
/// <summary>对 Key 升序排序。</summary>
public static List<KeyValuePair<TKey, TValue>> KeysAscend<TKey, TValue>(Dictionary<TKey, TValue> target) where TKey : IComparable<TKey> { return Keys(target, new Comparison<TKey>(AscendT)); }
/// <summary>对 Key 升序排序。</summary>
public static List<KeyValuePair<TKey, TValue>> KeysDescend<TKey, TValue>(Dictionary<TKey, TValue> target) where TKey : IComparable<TKey> { return Keys(target, new Comparison<TKey>(DescendT)); }
/// <summary>对 Key 升序排序。</summary>
public static Dictionary<string, string> SortKeyAscend(Dictionary<string, string> dictionary) { return SortHelper.DictionaryStringString(dictionary, true, true); }
/// <summary>对 Key 降序排序。</summary>
public static Dictionary<string, string> SortKeyDecend(Dictionary<string, string> dictionary) { return SortHelper.DictionaryStringString(dictionary, true, false); }
/// <summary>对 Value 升序排序。</summary>
public static Dictionary<string, string> SortValueAscend(Dictionary<string, string> dictionary) { return SortHelper.DictionaryStringString(dictionary, false, true); }
/// <summary>对 Value 降序排序。</summary>
public static Dictionary<string, string> SortValueDecend(Dictionary<string, string> dictionary) { return SortHelper.DictionaryStringString(dictionary, false, false); }
/// <summary>对 Key 升序排序。</summary>
public static Dictionary<string, double> SortKeyAscend(Dictionary<string, double> dictionary) { return SortHelper.DictionaryStringDouble(dictionary, true, true); }
/// <summary>对 Key 降序排序。</summary>
public static Dictionary<string, double> SortKeyDecend(Dictionary<string, double> dictionary) { return SortHelper.DictionaryStringDouble(dictionary, true, false); }
/// <summary>对 Value 升序排序。</summary>
public static Dictionary<string, double> SortValueAscend(Dictionary<string, double> dictionary) { return SortHelper.DictionaryStringDouble(dictionary, false, true); }
/// <summary>对 Value 降序排序。</summary>
public static Dictionary<string, double> SortValueDecend(Dictionary<string, double> dictionary) { return SortHelper.DictionaryStringDouble(dictionary, false, false); }
#endregion
}
}
|