none
sorting RRS feed

  • Question


  • using System;

    using System.Collections.Generic; namespace sorting { class Program { static void Main(string[] args) { DateTime datum = DateTime.UtcNow; Console.WriteLine(datum.ToString()); List<string[]> logBook = new List<string[]>(); Console.WriteLine("\tWelcome to the log book\n"); Console.ReadLine(); bool kör = true; while (kör) { Console.WriteLine("\n\t[1]Print all entries\t"); Console.WriteLine("\t[2]Write new entry"); Console.WriteLine("\t[3]Sort all the entries"); Console.WriteLine("\t[4]Search an entry"); Console.WriteLine("\t[5]End program"); Console.Write("\tChoose: "); int choice; Int32.TryParse(Console.ReadLine(), out choice); switch (choice) { case 1: int max = logBook.Count; if (max > 0) { for (int i = 0; i < max; i++) { Console.WriteLine("\t" + i + " " + "Title: " + logBook[i][0] + "\n\tText: " + logBook[i][1]); } } else Console.WriteLine("\tThe log book is empty"); Console.ReadLine(); break; case 2: Console.Write("\n\tWrite the title: "); string title = Console.ReadLine(); Console.Write("\n\tWrite the text here: "); string text = Console.ReadLine(); string[] post = new string[] { title, text }; logBook.Add(post); break; case 3: max = logBook.Count; char[] titlePost = logBook[i][0];//I don't understand why the "i" here isn't recognized if (max > 2) { for (int i = 0; i < logBook.Count; i++) { var nrLeft = max - 1; for (int j = 0; j < nrLeft; j++) { if (titlePost[j] > titlePost[j + 1]) { int temp = titlePost[j]; titlePost[j] = titlePost[j + 1]; temp = titlePost[j + 1]; } } } for (int i = 0; i < max; i++) { Console.WriteLine("\ti" + " " + logBook[i][0]); } } else Console.WriteLine("\tThere is only one entry in the log book" + logBook[1][0]); Console.ReadLine(); break; } } } } }

    Hello,

    Iam very newbie in programming and i have a task which shown above.  I am trying to sort entries and i dont know if I did right in using char to alphabetically arrange the saved entries.  I dont understand why the logBook[i][0] doesn't work in case 3.  I also tried to save a 'date' as one of the arrays but i cant get wrap my head around it.  

    I really appreciate any help I could get.  Thank you in advance.

    -Morena

    Tuesday, December 24, 2019 8:31 AM

Answers


  • switch (choice) { case 1: int max = logBook.Count; if (max > 0) { for (int i = 0; i < max; i++) { Console.WriteLine("\t" + i + " " + "Title: " + logBook[i][0] + "\n\tText: " + logBook[i][1]); } } else Console.WriteLine("\tThe log book is empty"); Console.ReadLine(); break; case 2: Console.Write("\n\tWrite the title: "); string title = Console.ReadLine(); Console.Write("\n\tWrite the text here: "); string text = Console.ReadLine(); string[] post = new string[] { title, text }; logBook.Add(post); break; case 3: max = logBook.Count; char[] titlePost = logBook[i][0];//I don't understand why the "i" here isn't recognized


     I dont understand why the logBook[i][0] doesn't work in case 3. 

    Because the variable i isn;t declared anywhere within the scope of this line
    of code. In case 1 that variable is defined in the for statement:

    for (int i = 0; i < max; i++)

    So that variable exists only until that for statement ends at the closing
    right brace.

    - Wayne

    • Marked as answer by MorenainHanoi Tuesday, December 24, 2019 8:44 PM
    • Unmarked as answer by MorenainHanoi Tuesday, December 24, 2019 8:51 PM
    • Marked as answer by MorenainHanoi Wednesday, December 25, 2019 9:24 AM
    Tuesday, December 24, 2019 2:03 PM
  • Hi ,

    What Wayne said is completely correct, that's the reason, you put logBook [i] [0] in a for loop in case1, and the for loop declared the variable i, so there was no error.

    But in case3, logBook [i] [0] appears outside the for loop, so the program doesn't know what “i” is.

    You should put this line in the for loop below, and then change this line to

    char [] titlePost = logBook [i] [0 ] .ToCharArray ();

    The error will disappear.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Wednesday, December 25, 2019 5:14 AM

All replies

  • Hello,

    Please provide what a list would look like before and after sorting. I don't see anything in code that works with a date. Also, are you only working with in memory data is that all or do you intend at some point to save this data and read it back?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, December 24, 2019 9:58 AM
    Moderator
  • Hi MorenainHanoi,

    Thank you for posting here.

    For your question, you should put this code in the following loop, and logBook [i] [0] is a string. If you want to convert it to char [], you need to call ToCharArray().

    Besides, I don't think using string [] to accept input parameters is a good idea, you can create a class to replace it.

    Finally, your comparison method is inappropriate. I think you want to compare different titles, don't you?
    I replaced it with a short statement.

    Here is the entire code:

    using System;
    using System.Collections.Generic;
    
    namespace sorting
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime datum = DateTime.UtcNow;
                Console.WriteLine(datum.ToString());
    
                List<MyClass> logBook = new List<MyClass>();
    
                Console.WriteLine("\tWelcome to the log book\n");
    
                bool kör = true;
                while (kör)
                {
                    Console.WriteLine("\n\t[1]Print all entries\t");
                    Console.WriteLine("\t[2]Write new entry");
                    Console.WriteLine("\t[3]Sort all the entries");
                    Console.WriteLine("\t[4]Search an entry");
                    Console.WriteLine("\t[5]End program");
                    Console.Write("\tChoose: ");
                    int choice;
                    Int32.TryParse(Console.ReadLine(), out choice);
    
                    switch (choice)
                    {
                        case 1:
                            int max = logBook.Count;
                            if (max > 0)
                            {
                                for (int i = 0; i < max; i++)
                                {
                                    Console.WriteLine("\t" + i + " "
                                        + "Title: " + logBook[i].Title
                                        + "\n\tText: " + logBook[i].Text);
                                }
                            }
                            else
                                Console.WriteLine("\tThe log book is empty");
                            Console.ReadLine();
                            break;
                        case 2:
                          
                            Console.Write("\n\tWrite the title: ");
                            string title = Console.ReadLine();
                       
                            Console.Write("\n\tWrite the text here: ");
                            string text = Console.ReadLine();
    
                            MyClass myClass = new MyClass() { Title = title, Text = text };
                            logBook.Add(myClass);
                            break;
                        case 3:
                            max = logBook.Count;
    
                            if (max >= 2)
                            {
                                for (int i = 0; i < logBook.Count; i++)
                                {
                                    logBook.Sort(new Comparison<MyClass>((x, y) => String.Compare(x.Title, y.Title)));
    
                                }
                                for (int i = 0; i < max; i++)
                                {
                                    Console.WriteLine("\t"+i + " " + logBook[i]);
                                }
                            }
    
                            else
                                Console.WriteLine("\tThere is only one entry in the log book" + logBook[0]);
                            Console.ReadLine();
                            break;
                    }
                }
            }
        }
        class MyClass
        {
           public string Title { get; set; }
           public string Text { get; set; }
            public override string ToString()
            {
                return Title + " " + Text; 
            }
        }
    }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 24, 2019 10:05 AM
  • Hey Timon,

    Thanks for helping and with this class.  I think we were specifically instructed not to use class, so this one logBook[i][0] in case 3 is the only one causing the problem.  I don't know why it doesn't recognize when in case 1 it works perfectly even without any entries yet.

    -Morena

    Tuesday, December 24, 2019 10:17 AM
  • Hey Karen,

    I haven't done any string date but I would love to.  I just dont know how to add date in one of the arrays as a string.

    -Morena

    Tuesday, December 24, 2019 10:20 AM

  • switch (choice) { case 1: int max = logBook.Count; if (max > 0) { for (int i = 0; i < max; i++) { Console.WriteLine("\t" + i + " " + "Title: " + logBook[i][0] + "\n\tText: " + logBook[i][1]); } } else Console.WriteLine("\tThe log book is empty"); Console.ReadLine(); break; case 2: Console.Write("\n\tWrite the title: "); string title = Console.ReadLine(); Console.Write("\n\tWrite the text here: "); string text = Console.ReadLine(); string[] post = new string[] { title, text }; logBook.Add(post); break; case 3: max = logBook.Count; char[] titlePost = logBook[i][0];//I don't understand why the "i" here isn't recognized


     I dont understand why the logBook[i][0] doesn't work in case 3. 

    Because the variable i isn;t declared anywhere within the scope of this line
    of code. In case 1 that variable is defined in the for statement:

    for (int i = 0; i < max; i++)

    So that variable exists only until that for statement ends at the closing
    right brace.

    - Wayne

    • Marked as answer by MorenainHanoi Tuesday, December 24, 2019 8:44 PM
    • Unmarked as answer by MorenainHanoi Tuesday, December 24, 2019 8:51 PM
    • Marked as answer by MorenainHanoi Wednesday, December 25, 2019 9:24 AM
    Tuesday, December 24, 2019 2:03 PM
  • Hi ,

    What Wayne said is completely correct, that's the reason, you put logBook [i] [0] in a for loop in case1, and the for loop declared the variable i, so there was no error.

    But in case3, logBook [i] [0] appears outside the for loop, so the program doesn't know what “i” is.

    You should put this line in the for loop below, and then change this line to

    char [] titlePost = logBook [i] [0 ] .ToCharArray ();

    The error will disappear.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Wednesday, December 25, 2019 5:14 AM
  • Thank you Wayne and Timon for the big help :)

    -Morena

    Wednesday, December 25, 2019 9:25 AM
  • Thank you Timon :)  It really fixes the problem.  Struggling with the sorting still but i'm hoping that I will sort them out soon.

    -Morena

    Wednesday, December 25, 2019 9:28 AM