Sort based on given order with no repeating code

static void Main(string[] args)
        {
           
            Console.WriteLine("Enter How many numbers you want for Sort:-");
            int arrLength = Convert.ToInt32(Console.ReadLine());

            int[] userInputs = new int[arrLength];
           

            Console.WriteLine("Enter List of  {0} Integer items to sort:" ,arrLength);
            for(int i=0;i<arrLength ; i ++)
            {
                userInputs[i] =Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("Enter whcih order you want to sort:");
            Console.WriteLine("\t Press '0' for Descending | \t '1' for Ascending:");
            int order = Convert.ToInt32(Console.ReadLine());
           
            userInputs = GetSortedData(userInputs, order);

            Console.WriteLine("The Sorted result is as below:");
            for (int num = 0; num < arrLength; num++)
            {
              Console.Write(" "+ userInputs[num]+" ");
            }
            Console.ReadLine();

        }



        /// <summary>
        /// This method sorts your input array on your specified order.
        /// </summary>
        /// <param name="inputArray"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public static int[] GetSortedData(int[] inputArray, int order)
        {
            for (int i = 0; i < inputArray.Length - 1; i++)
            {
                for (int j = 0; j < inputArray.Length - 1; j++)
                {
                    if (order == 0? inputArray[j] < inputArray[j + 1] : inputArray[j] > inputArray[j + 1])
                    {
                        int temp = inputArray[j];
                        inputArray[j] = inputArray[j + 1];
                        inputArray[j + 1] = temp;
                    }               
                }           
            }
            return inputArray;
        }

1 comment:

  1. The importance of ASP.NET (ASP Dot Internet) Web Development has recently improved a whole lot due to higher demand of active websites and the need of developing advance online programs. This Microsoft dot net framework software facilitates the creators to formulate purposes that web page link every one of the identities for any business such as employees, the and customers supervision. The need for it has elevated considerably particularly with guide to the World Wide Web setting. Within the Website development market it has come to be probably the most required Best Dot net training in Chennai. The companies offering web development services now offer ASP.NET web development and a range of other application development services to the corporate world for building tailored solutions according to their specific need.Also
    see my blog Dot Net Training Academy in Chennai

    ReplyDelete