-2

I'm a beginner at C# and I need some help regarding this problem. How do I make this code work? I want it to accept user input by entering names and grades and then get it automatically sorted. I tried and looked for many tutorials, but they don't cover the 2D array topic that sorts items that much.

For the part of Array.Sort(g); I can run this code but it gets error on this part.

I appreciate any help and correction :)


namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] g = new string[10,2];
            Console.WriteLine("Names and their Prelim GWA: ");


            for (int i = 0; i <= 10; i++)
            {
                Console.ReadLine();
            }
            Array.Sort(g);

            Console.ReadKey();

        }
    }
}

Dean
  • 1
  • *"Hot to sort multidimensional array?"* answered here: https://stackoverflow.com/questions/232395/how-do-i-sort-a-two-dimensional-rectangular-array-in-c – Auditive Nov 05 '21 at 14:26

1 Answers1

0

Try this:

static void Main(string[] args)
    {
        List<student> Students = new List<student>;

        Console.WriteLine("Names and their Prelim GWA: ");

        for (int i = 0; i <= 10; i++)
        {
           Console.Writline("Enter student " + i + " name and grade on separate lines:")
           string name = Console.ReadLine();
           decimal grade = Convert.ToDecimal(Console.ReadLine());
           
           Students.Add(new student() { Name = name, Grade = grade }
        }
        Students = Students.OrderBy(x => x.Grade).ToList();

        //Now the students are sorted by grade.

    }
    private class student
    {
       public string Name;
       public decimal Grade;