Thursday, November 8, 2018

C# program to sort names in ascending and descending order

আমার কাছে userName এর একটা string আছে যেখানে প্রতিটি নাম semicolon দিয়ে seperated। আমি C# এ একটা program লিখতে চাই যেটি আমার userName গুলকে ascending এবং descending order এ short করে দেবে।

string strUserNames = "Rob;Mike;Able;Sara;Peter;John;Tom;Ben";



using System;
namespace MainPrograms
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Prompt the user to enter the list of usernames
            Console.WriteLine("Please enter the Name list separated by semicolon");

            // Read the user name list from the console
            string InputedNameList = Console.ReadLine();


            // Sampe list of user names that can be used as an input
            // strUserNames = "Rob;Mike;Able;Sara;Peter;John;Tom;Ben";


            // Split the string into a string array based on semi colon
            string[] InputedNameArray = InputedNameList.Split(';');


            // Print the names before sorting using foreach loop
            Console.WriteLine("NameList Before Sorting");
            foreach (var item in InputedNameArray)
            {
                Console.WriteLine("{0}",item);

            }


            // Sort the elements in the array in ascending order
            Array.Sort(InputedNameArray);

            // Print the elements of the array after sorting
            Console.WriteLine("NameList After Sorting in Assending Order");
            foreach (var item in InputedNameArray)
            {
                Console.WriteLine("{0}", item);

            }


            // Reverse the elements in the sorted array to get
            // the elements in descending order
            Array.Reverse(InputedNameArray);

            // Finally print the elements
            Console.WriteLine("NameList After Sorting in Decending Order");
            foreach (var item in InputedNameArray)
            {
                Console.WriteLine("{0}", item);

            }
            Console.ReadLine();

        }
    }
}

No comments:

Post a Comment