Exemplo n.º 1
0
        /// <summary>
        /// Sorts the elements in a range of elements in an array using the specified sort expression.
        /// </summary>
        /// <typeparam name="T">Type of elements in array</typeparam>
        /// <param name="array">Array to be sorted.</param>
        /// <param name="index">Index of first element to be sorted</param>
        /// <param name="length">Number of elements to be sorted.</param>
        /// <param name="sortExpression">A SQL-like sort expression with comma separated property names (and optional direction specifiers) (e.g. "Age DESC, Name")</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="array"/> is null or <paramref name="sortExpression"/> is null</exception>
        /// <exception cref="ParserException">If <paramref name="sortExpression"/> is an invalid sort expression.</exception>
        /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="index"/> or <paramref name="length" /> out of range.</exception>
        public static void Sort <T>(this T[] array, int index, int length, String sortExpression)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }
            if (sortExpression == null)
            {
                throw new ArgumentNullException("sortExpression");
            }
            IComparer <T> comparer = ComparerBuilder <T> .CreateTypeComparer(sortExpression);

            Array.Sort(array, index, length, comparer);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sort the elements of a sequence according to the specified textual sort expression.
        /// </summary>
        /// <typeparam name="T">Type in sequence</typeparam>
        /// <param name="source">Sequence to be sorted</param>
        /// <param name="sortExpression">A SQL-like sort expression with comma separated property names (and optional direction specifiers) (e.g. "Age DESC, Name")</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="source"/> is null or <paramref name="sortExpression"/> is null</exception>
        /// <exception cref="ParserException">If <paramref name="sortExpression"/> is an invalid sort expression.</exception>
        public static IEnumerable <T> OrderBy <T>(this IEnumerable <T> source, String sortExpression)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (sortExpression == null)
            {
                throw new ArgumentNullException("sortExpression");
            }
            IComparer <T> comparer = ComparerBuilder <T> .CreateTypeComparer(sortExpression);

            IEnumerable <T> orderedEnum = source.OrderBy <T, T>(x => x, comparer);

            return(orderedEnum);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sorts the elements in a range of elements in a list using the specified sort expression
        /// </summary>
        /// <typeparam name="T">List item type</typeparam>
        /// <param name="list">List to be sorted</param>
        /// <param name="index">Index of first item to be sorted</param>
        /// <param name="count">Number of items to be sorted.</param>
        /// <param name="sortExpression">A SQL-like sort expression with comma separated property names (and optional direction specifiers) (e.g. "Age DESC, Name")</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="list"/> is null or <paramref name="sortExpression"/> is null</exception>
        /// <exception cref="ParserException">If <paramref name="sortExpression"/> is an invalid sort expression.</exception>
        /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="index"/> not a valid list index or <paramref name="count"/> out of range.</exception>
        public static void Sort <T>(this List <T> list, int index, int count, String sortExpression)
        {
            IComparer <T> comparer = ComparerBuilder <T> .CreateTypeComparer(sortExpression);

            list.Sort(index, count, comparer);
        }