I have a string of user names seperated by semi colon. I want a c# program that can sort these names in both ascending and descending order.
string strUserNames = "Rob;Mike;Able;Sara;Peter;John;Tom;Ben";
string strUserNames = "Rob;Mike;Able;Sara;Peter;John;Tom;Ben";
using System;
namespace SamplePrograms{ class SortNamesInAscendingAndDescendingOrder { public static void Main() { // Prompt the user to enter the list of user names Console.WriteLine("Please enter list of names seperated by semi colon"); // Read the user name list from the console string strUserNames = 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[] arrUsersNames = strUserNames.Split(';');
// Print the names before sorting using foreach loop Console.WriteLine("Names before sorting"); foreach (string UserName in arrUsersNames) { Console.WriteLine(UserName); }
// Sort the elements in the array in ascending order Array.Sort(arrUsersNames);
// Print the elements of the array after sorting Console.WriteLine("Names after sorting in ascending order"); foreach (string UserName in arrUsersNames) { Console.WriteLine(UserName); }
// Reverse the elements in the sorted array to get // the elements in descending order Array.Reverse(arrUsersNames); // Finally print the elements Console.WriteLine("Names after sorting in descending order"); foreach (string UserName in arrUsersNames) { Console.WriteLine(UserName); }
Console.ReadLine(); } }}
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.