using System; using System.Collections.Generic; using JM.LinqFaster.Utils; namespace JM.LinqFaster { public static partial class LinqFaster { /// /// Sorts the elements of a sequence in ascending order according to a key. /// Unlike standard Linq NOT a stable sort. /// /// A sequence of values to order. /// A function to extract a key from an element. /// A Comparer to compare keys. /// A sequence whose alements are ordered according to a key public static TSource[] OrderByF(this TSource[] source, Func keySelector,IComparer comparer = null) { if (source == null) { throw Error.ArgumentNull("source"); } if (keySelector == null) { throw Error.ArgumentNull("keySelector"); } if (comparer == null) { comparer = Comparer.Default; } var keys = new TKey[source.Length]; for (int i = 0; i < keys.Length; i++) { keys[i] = keySelector(source[i]); } var result = (TSource[])source.Clone(); Array.Sort(keys,result,comparer); return result; } /// /// Sorts the elements of a sequence in descending order according to a key. /// Unlike standard Linq NOT a stable sort. /// /// A sequence of values to order. /// A function to extract a key from an element. /// A Comparer to compare keys. /// A sequence whose alements are ordered according to a key public static TSource[] OrderByDescendingF(this TSource[] source, Func keySelector, IComparer comparer = null) { if (source == null) { throw Error.ArgumentNull("source"); } if (keySelector == null) { throw Error.ArgumentNull("keySelector"); } if (comparer == null) { comparer = Comparer.Default; } var keys = new TKey[source.Length]; for (int i = 0; i < keys.Length; i++) { keys[i] = keySelector(source[i]); } var result = (TSource[])source.Clone(); Array.Sort(keys, result, comparer.Reverse()); return result; } // ---------------------- Lists /// /// Sorts the elements of a sequence in ascending order according to a key. /// Unlike standard Linq NOT a stable sort. /// /// A sequence of values to order. /// A function to extract a key from an element. /// A Comparer to compare keys. /// A sequence whose alements are ordered according to a key public static List OrderByF(this List source, Func keySelector, IComparer comparer = null) { if (source == null) { throw Error.ArgumentNull("source"); } if (keySelector == null) { throw Error.ArgumentNull("keySelector"); } if (comparer == null) { comparer = Comparer.Default; } var result = new List(source); var lambdaComparer = new LambdaComparer(keySelector, comparer); result.Sort(lambdaComparer); return result; } /// /// Sorts the elements of a sequence in descending order according to a key. /// Unlike standard Linq NOT a stable sort. /// /// A sequence of values to order. /// A function to extract a key from an element. /// A Comparer to compare keys. /// A sequence whose alements are ordered according to a key public static List OrderByDescendingF(this List source, Func keySelector, IComparer comparer = null) { if (source == null) { throw Error.ArgumentNull("source"); } if (keySelector == null) { throw Error.ArgumentNull("keySelector"); } if (comparer == null) { comparer = Comparer.Default; } var result = new List(source); var lambdaComparer = new ReverseLambdaComparer(keySelector, comparer); result.Sort(lambdaComparer); return result; } } }