How to sort an array of integers in ascending or descending order using c# .net

Consider we have an array of integers  int [] arr={90,60,20,30,100}  we need to format it in ascending order like this  20,30,60,90,100  so what we have to do?
we need to use two for loops and one temp variable using first loop it will send first index value of the array to the last, then inside we have given the second for loop it will do the  sorting so it will compare with next place value and  assign the small value to the temp variable and move the large value to second place so our syntax look like this in second for loop
 declare a temp variable of type integer outside for loops.

                 if (arr[sorts] > arr[sorts + 1])
                    {
                        temp = arr[sorts + 1];
                        arr[sorts + 1] = arr[sorts];
                        arr[sorts] = temp;
                    }

In our case arr[sorts]=90 and arr[sort+1]=60  so it will assign 60 into temp
temp=60 and arr[sort+1]=90 and arr[sorts]=60
so our current list become 60,90, then our for loop pass 20 now our arr[sorts]=90 and arr[sort+1]=20
it will follow the procedure until the loop exit
so we will get current array like this 60,20,30,90,100
then again it will go to the first for loop increment the count to 1 and it will sort the array again. it will continue till the first for loop exit,by the time we will get our array sorted.
here in our case we will get sorted array in second stage itself
20,30,60,90,100
 for getting it in descending order change the if condition like this
if(arr[sorts]<arr[sorts+1])

Console Application for getting sorting array in ascending order




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shape
{
    class sort
    {

        static void Main(string[] args)
        {
            
            Console.WriteLine("Give first num and press enter then seceond vice versa");
            Console.WriteLine("Please Enter the 5 Integers need to sort");
            int[] arr= new int[5];

            for (int d = 0; d < arr.Length; d++)
            {
                arr[d] =Convert.ToInt32(Console.ReadLine());
            }
            

            int temp = 0;

            for (int j = 0; j < arr.Length - 1; j++)
            {
                for (int sorts = 0; sorts < arr.Length - 1; sorts++)
                {
                    if (arr[sorts] > arr[sorts + 1])
                    {
                        temp = arr[sorts + 1];
                        arr[sorts + 1] = arr[sorts];
                        arr[sorts] = temp;
                    }

                }

            }
            Console.WriteLine("sorted array");

            for (int i = 0; i < arr.Length; i++)
                Console.Write(arr[i] + " ");
               
                Console.ReadLine();
                
        }
    }
}




So Now we can check the code if we are using Linq how simple is the sorting no need to use temp concept at all.

It will write sorted  array in descending order.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shape
{
    class Linqex
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Give first num and press enter then seceond vice versa");
            Console.WriteLine("Please Enter the 5 Integers need to sort");
            int[] arr = new int[5];

            for (int d = 0; d < arr.Length; d++)
            {
                arr[d] = Convert.ToInt32(Console.ReadLine());
            }

            Console.WriteLine("Sorted array :");

                 arr.OrderByDescending(i=>i)
                .ToList()
                .ForEach(i=>Console.WriteLine(i));
        }
    }
}




if you want sorted array  in ascending order
change  arr.OrderByDescending(i=>i)      into arr.OrderBy(i=>i)    

0 comments: