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