Search code examples
c#stringformatalphabeticalsortedlist

C# - How to sort and arrange literal UserValue in alphabetical order


Quick overview: Working in IT, currently gaining an understanding of software development and my placement manager has set me some "basic" (excessively hard given my coding knowledge) questions to find a solution too.

Set Question: Write a console application that inputs three characters and writes then out in alphabetical order. The program shall continue until no longer needed.

Placement managers notes of help: Ask the user to enter a string of 3 letters - Store the input as string - Convert the string to lowercase - Create 3 Char variables and split the string so each character in the string is in its own char variable - Get the ascii value of the three different Char variables storing them in int variables - When you have the ascii values you should be able to compare them and then output them in order.

My current code:

static void Main(string[] args)
    {
        string input; 

        bool displayMenu = true;
        while (displayMenu == true)
        {
            displayMenu = MainMenu();
        }
    }

    private static bool MainMenu()
    {
        Console.Clear();
        Console.WriteLine("Choose an Option: ");
        Console.WriteLine("1) Alphabetical Organiser");
        Console.WriteLine("2) Exit Application");
        string result = Console.ReadLine();

        if (result == "1")
        {
            alphaOrganiser();
            return true;
        }
        else if (result == "2") 
        {
            Console.Clear();
            Console.WriteLine("GoodBye!");
            Console.ReadLine();
            return false;
        }
        else
        {
            return true;
        }
    }
    public static void alphaOrganiser()
    {
        Console.Clear();
        Console.WriteLine("Alphabbetical Organiser!");
        Console.WriteLine("Please enter 3 alphabetical characters into the console : ");
        string input = Console.ReadLine();
        Console.Write("You inputed : " + input);

        Console.ReadKey();

Current problems: I can get the console to present the userValue (input) but how do I get the console to read this data, then sort it (i have tried the command sortedList but cant seem to format it correctly?) and THEN present the manipulated code to the user?

Any feedback would be appreciated, any learning opportunities, links or further explanation detail would also be helpful.

Thanks in advance for all responses.


Solution

  • There are many ways of do Iterating the input string and working with chars directly whould have better perfomance, but this example is more concise:

    Substitute your lines:

        string input = Console.ReadLine();
        Console.Write("You inputed : " + input);
    

    by

        string input = Console.ReadLine();
        List<char> sorted = new List<char>(input.ToCharArray());
        sorted.Sort();
        Console.WriteLine(String.Join("", sorted));