---
title: "How to sort object arrays by specific property in C#?"  
description: "How to sort object arrays by specific property in C#?"  
author: "Steilla Mitchel"  
published: 2024-06-11  
updated: 2024-06-11  
canonical: https://www.mindstick.com/forum/160713/how-to-sort-object-arrays-by-specific-property-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 3 minutes  

---

# How to sort object arrays by specific property in C#?

How to sort object [arrays](https://www.mindstick.com/articles/11955/arrays-and-its-limitations) by [specific property](https://www.mindstick.com/forum/158770/write-a-python-program-to-sort-a-list-of-objects-based-on-a-specific-property) in C#?

## Replies

### Reply by Ravi Vishwakarma

You can sort an array of objects by a specific [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) using LINQ's OrderBy or OrderByDescending method. \
Here's how you can do it:

Let's say you have an array of objects of type Student, and you want to sort them by their Name property:\
**Step 1:** Create `Student` class to apply to sort.

```cs

using System.Reflection;
using System.Text;

namespace ConsoleApp1
{
    // Define the Student class
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Grade { get; set; }
        public float? Marks { get; set; }
        public string FatherName { get; set; }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder("");
            foreach (PropertyInfo property in this.GetType().GetProperties())
            {
                sb.AppendFormat("{0}: {1} ", property.Name, property.GetValue(this));
            }
            return sb.ToString();
        }
    }
}
```

**Step 2.** Let's take an example of sorting using predefined methods in LINQ

```cs
class Program
{
    static void Main()
    {
        // Sample array of Student objects
        Student[] students = {
            new Student { Id = 1, Name = "Alice ", Age = 20 },
            new Student { Id = 2, Name = "Bob", Age = 22 },
            new Student { Id = 3, Name = "Charlie", Age = 21 }
        };

        // Sort the array of students by Name
        Student[] sortedStudents = students.OrderBy(student => student.Name).ToArray();

        // Print the sorted array
        foreach (var student in sortedStudents)
        {
            Console.WriteLine($"Id: {student.Id}, Name: {student.Name}, Age: {student.Age}");
        }
    }
}
```

In the code above:

1. We have an array of Student objects named students.
2. We use LINQ's OrderBy method to sort the students array based on the Name property.
3. The sorted array is stored in sortedStudents.
4. Finally, we print the sorted array to the console.

If you want to sort in descending order, you can use the OrderByDescending method instead of OrderBy.

## For example:

```cs
Student[] sortedStudents = students.OrderByDescending(student => student.Name).ToArray();
```

**Step 3.** Let's create a Custom Comparator in C#

```cs
using ConsoleApp1;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main()
        {
            // Sample array of Student objects
            Student[] students = {
                new Student { Id = 3, Name = "Charlie", Age = 21 },
                new Student { Id = 2, Name = "Bob", Age = 22 },
                new Student { Id = 1, Name = "Alice ", Age = 20 }
            };

            //Using Array.Sort with Comparison Delegate
            //Array.Sort<Student>(students, (x, y) => string.Compare(x.Name, y.Name));

            //Using LINQ OrderBy Method
            //Student[] sortedStudents = students.OrderBy(student => student.Name).ToArray();

            //Using LINQ OrderByDescending method desending order data
            //Student[] sortedStudentsOrderByDescending = students.OrderByDescending(student => student.Name).ToArray();

            //Compare
            Array.Sort<Student>(students, new CustomComparer());

            // Print the sorted array
            foreach (var student in students)
            {
                Console.WriteLine($"Id: {student.Id}, Name: {student.Name}, Age: {student.Age}");
            }
            // Wait for the user to press a key before closing (not reached due to return above)
            Console.ReadLine();
        }
    }
}

public class CustomComparer : IComparer<Student>
{
    public int Compare(Student x, Student y)
    {
        // Custom comparison logic
        return string.Compare(x.Name, y.Name);
    }
}
```


---

Original Source: https://www.mindstick.com/forum/160713/how-to-sort-object-arrays-by-specific-property-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
