Ejemplo n.º 1
0
        /// <summary>
        /// sort array strings in an array by min element of string
        /// </summary>
        /// <param name="arr">array</param>
        /// <param name="typeSort">type of sort: may be ascending or descending</param>
        private static void SortStringsArrayByMinElement(int[][] arr, AscDescSort typeSort)
        {
            for (var i = 0; i < arr.Length; i++)
            {
                for (var k = i + 1; k < arr.Length; k++)
                {
                    var maxElement1 = GetMinElement(arr, i);
                    var maxElement2 = GetMinElement(arr, k);

                    if (typeSort == AscDescSort.Ascending)
                    {
                        if (maxElement1 > maxElement2)
                        {
                            ReplaceArrayElements(arr, i, k);
                        }
                    }
                    else
                    {
                        if (maxElement1 < maxElement2)
                        {
                            ReplaceArrayElements(arr, i, k);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sort an array by sum of string elements
        /// </summary>
        /// <param name="arr">array</param>
        /// <param name="typeSort">type of sort: may be ascending or descending</param>
        private static void SortStringsArrayBySumElements(int[][] arr, AscDescSort typeSort)
        {
            for (var i = 0; i < arr.Length; i++)
            {
                for (var k = i + 1; k < arr.Length; k++)
                {
                    var sum1 = 0;

                    for (var j = 0; j < arr[i].Length; j++)
                    {
                        sum1 += arr[i][j];
                    }

                    var sum2 = 0;
                    for (var t = 0; t < arr[k].Length; t++)
                    {
                        sum2 += arr[k][t];
                    }

                    if (typeSort == AscDescSort.Ascending)
                    {
                        if (sum1 > sum2)
                        {
                            ReplaceArrayElements(arr, i, k);
                        }
                    }
                    else
                    {
                        if (sum1 < sum2)
                        {
                            ReplaceArrayElements(arr, i, k);
                        }
                    }
                }
            }
        }