You can sort an array of objects by a specific property 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.
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
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:
We have an array of Student objects named students.
We use LINQ's OrderBy method to sort the students array based on the Name property.
The sorted array is stored in sortedStudents.
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.
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);
}
}
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
You can sort an array of objects by a specific property 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
Studentclass to apply to sort.Step 2. Let's take an example of sorting using predefined methods in LINQ
In the code above:
If you want to sort in descending order, you can use the OrderByDescending method instead of OrderBy.
For example:
Step 3. Let's create a Custom Comparator in C#