Esempio n. 1
0
        /// <summary>
        /// Sorts rows of given array.
        /// </summary>
        /// <param name="array">Sourse array.</param>
        /// <param name="comparer">Row comparer.</param>
        /// <param name="ascending">Sorting order. Ascending by default.</param>
        public static void SortRows(int[,] array, IRowComparer comparer, bool ascending = true)
        {
            int length = array.GetLength(0);

            if (length == 0)
            {
                throw new ArgumentOutOfRangeException("Array is empty");
            }

            if (comparer == null)
            {
                throw new ArgumentNullException(nameof(comparer));
            }

            for (int i = 0; i < length - 1; i++)
            {
                for (int j = 0; j < length - i - 1; j++)
                {
                    if (comparer.CompareRows(array, j, j + 1) == ascending)
                    {
                        array.SwapRows(j, j + 1);
                    }
                }
            }
        }