Sorting an array of objects in C# (Noob)

braBenj

Member
Joined
Jun 1, 2015
Messages
27
Reaction score
0
Location
Pretoria, Johannesburg
Hello MyBB.

I'm having some trouble with the following code.

Code:
     class Students
    {
       public string Name { set; get; }
       public int stuNum { set; get; }
        static void Main(string[] args)
        {
            Students[] studentArray = new Students[4];
            for (int i = 0; i < studentArray.Length; i++)
            {
                studentArray[i] = new Students();
            }

            studentArray[0].Name = "cccc";
            studentArray[1].Name = "aaaa";
            studentArray[2].Name = "bbbb";
            studentArray[3].Name = "dddd";

            studentArray[0].stuNum = 4444;
            studentArray[1].stuNum = 2222;
            studentArray[2].stuNum = 3333;
            studentArray[3].stuNum = 1111;        

        }
    }

I'm trying to figure out how to sort the objects via Name or student number. Simply using the "Array.Sort(studentArray)" method understandably does not work. How do I specify which fields I want to sort the objects by?

I'm new to C# and really new to OOP concepts so I apologize if I haven't phrased my question properly, but I hope you get the idea.
 
var sorted = studentArray.OrderBy(s => s.Name).ThenBy(s => s.stuNum);
 
Code:
        class Students
        {
            public string Name { set; get; }
            public int stuNum { set; get; }
            static void Main(string[] args)
            {
                Students[] studentArray = new[] {
							new Students { Name ="cccc", stuNum = 4444},
							new Students { Name ="aaaa", stuNum = 2222},
							new Students { Name ="bbbb", stuNum = 3333},
							new Students { Name ="dddd", stuNum = 1111},
							
						};
                studentArray = studentArray.OrderBy(a => a.Name).ToArray();
                studentArray.Dump(); //.Dump() is a LinqPad feature
            }
        }

Make sure you are have using System.Linq in your file.

.OrderBy will give you a IEnumerable that you can then convert back to an array with ToArray (if you really need it of that type).

I also showed you an easy way to initialize an array (or list) of complex types.
 
Hello MyBB.

I'm having some trouble with the following code.

Code:
     class Students
    {
       public string Name { set; get; }
       public int stuNum { set; get; }
        static void Main(string[] args)
        {
            Students[] studentArray = new Students[4];
            for (int i = 0; i < studentArray.Length; i++)
            {
                studentArray[i] = new Students();
            }

            studentArray[0].Name = "cccc";
            studentArray[1].Name = "aaaa";
            studentArray[2].Name = "bbbb";
            studentArray[3].Name = "dddd";

            studentArray[0].stuNum = 4444;
            studentArray[1].stuNum = 2222;
            studentArray[2].stuNum = 3333;
            studentArray[3].stuNum = 1111;        

        }
    }

I'm trying to figure out how to sort the objects via Name or student number. Simply using the "Array.Sort(studentArray)" method understandably does not work. How do I specify which fields I want to sort the objects by?

I'm new to C# and really new to OOP concepts so I apologize if I haven't phrased my question properly, but I hope you get the idea.

You are not doing such a bad job tbh, actually felt embarrassed that i'm doing it this way:-

public static get_Name(){
return this.Name
}

public static set_Name(string a){
this.Name = a;
}
 
Code:
        class Students
        {
            public string Name { set; get; }
            public int stuNum { set; get; }
            static void Main(string[] args)
            {
                Students[] studentArray = new[] {
							new Students { Name ="cccc", stuNum = 4444},
							new Students { Name ="aaaa", stuNum = 2222},
							new Students { Name ="bbbb", stuNum = 3333},
							new Students { Name ="dddd", stuNum = 1111},
							
						};
                studentArray = studentArray.OrderBy(a => a.Name).ToArray();
                studentArray.Dump(); //.Dump() is a LinqPad feature
            }
        }

Make sure you are have using System.Linq in your file.

.OrderBy will give you a IEnumerable that you can then convert back to an array with ToArray (if you really need it of that type).

I also showed you an easy way to initialize an array (or list) of complex types.

Thanks a lot! this worked perfectly, and it's a lot cleaner.
 
Use generic lists - there's no need to use arrays...
 
Its better to point him in a direction and let him figure it out.
 
Top
Sign up to the MyBroadband newsletter
X