Tuesday 18 September 2012

Arrange the string in ascending and descending order

Hi Friend, I hope you have enjoyed from my previous article .i.e When user enter numeric value ,print out the character values.
Here i will explain how to arrange string in ascending and descending order.
First open visual studio > open new project> click on console application > give the name of the application> click ok.

Code:

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//user to enter list of username
Console.WriteLine("please enter the name seperated by comma");
//read the user name from console
string username = Console.ReadLine();
// split the string into string array by comma
string[] arrusername = username.Split(',');
// print the name before sorting
Console.WriteLine("Name before sorting");
foreach(string userName in arrusername)
{
Console.WriteLine(userName);
}
// sort the element in ascending order
Array.Sort(arrusername);
// print the element after sorting
Console.WriteLine("string after sorting");
foreach(string userName in arrusername)
{
Console.WriteLine(userName);
}
// sort the element for descending order
Console.WriteLine("-------------------for Descending order");
Array.Reverse(arrusername);
// print the element in descending order
foreach(string userName in arrusername)
{
Console.WriteLine(userName);
}
Console.ReadLine();
}
}
}

I hope you will enjoy with this code.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.