Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now

Sort object array by specific property in C#

 

Sort object array by specific property in C#


In this Article we will learn how to sort a array which is created from the class property.

for an example we created a Student class like below

    public class Student

    {

        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

    }


Now created a method called SortArray and assign some value to this student class 

like below 

        public static void SortArray()

        {

 

  Student[] student = {

          new Student(){ FirstName="Munesh", LastName="sharma"},

          new Student(){ FirstName="DotNet", LastName="Office"},

          new Student(){ FirstName="panakj", LastName="kumar"}

           }; 

            var qry = from p in student

                      orderby p.LastName

                      select p; 

         Array.ForEach<Student>(qry.ToArray<Student>(), p => Console.WriteLine(p.FirstName + " " + p.LastName)); 

        }


output


Code sample



Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now