Friday 5 March 2010

Simple C# Code to Arrange arrays in Descending and Ascending Order.

// The Following Code arranges the Single-Dimensional Array in Descending Order.

C# Code to Arrange arrays in Descending and Ascending Order.


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

namespace ConsoleApplication1
{
class descend
{
static void Main(string[] args)
{
int[] num1 = new int[5] { 55,2,90,45,7};// Array Defined of 5 elements.
Array.Sort(num1); //Sorts the Array first.
Array.Reverse(num1);//Reverses the Array
foreach (int i in num1)// Loop applied for each element in the array
{
Console.WriteLine(i);// Output Is Displayed
}
}
}

// To Arrange the array in Ascending order the code is given below;

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

namespace ConsoleApplication1
{
class Ascend
{
static void Main(string[] args)
{
int[] num1 = new int[5] { 55,2,90,45,7};
Array.Sort(num1);
foreach (int i in num1)
{
Console.WriteLine(i);
}
}
}
}

Its the same as the code for arranging arrays in descending order ...except "Array.Reverse()" has been excluded from the code

.

// hope this helps you all.
}

2 comments: