Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

How to sort an array of objects by specific property in C#

Here, you will learn how to sort an array of objects by specific property in C#.

There are two ways you can sort an object array by a specific property, using Array.Sort() method and by using LINQ query.

class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
 
Person[] people = {
        new Person(){ FirstName="Steve", LastName="Jobs"},
        new Person(){ FirstName="Bill", LastName="Gates"},
        new Person(){ FirstName="Lary", LastName="Page"}
    };

The people array in the above example contains objects of Person class. You cannot use Array.Sort(people) because array contains objects, not primitive values.

Now, let's sort the above people array by the LastName property. For that, you need to create a class and implement IComparer interface, as shown below.

Example: Custom Comparer Class
class PersonComparer : IComparer
{
    public int Compare(object x, object y)
    {
        return (new CaseInsensitiveComparer()).Compare(((Person)x).LastName, ((Person)y).LastName);
    }
}

Now, we can sort an array using Array.Sort() method by specifying IComparer class.

Example: Sort Object Array
Person[] people = {
    new Person(){ FirstName="Steve", LastName="Jobs"},
    new Person(){ FirstName="Bill", LastName="Gates"},
    new Person(){ FirstName="Lary", LastName="Page"}
};
 
Array.Sort(people, new PersonComparer());
Try it

The same result can be achieved using LINQ query easily, as shown below.

Example: Sort using LINQ
Person[] people = {
    new Person(){ FirstName="Steve", LastName="Jobs"},
    new Person(){ FirstName="Bill", LastName="Gates"},
    new Person(){ FirstName="Lary", LastName="Page"}
};

var qry = from p in list
        orderby p.LastName
        select p;
 
Array.ForEach<Person>(qry.ToArray<Person>(), p =&gt; Console.WriteLine(p.FirstName + " " + p.LastName));
Try it
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

contact@tutorialsteacher.com

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.