C# Program to Sort a List of Employees Based on Salary using LINQ

In many software development projects, there comes a point where it becomes necessary to sort a list of objects based on one or more properties of the objects. In C#, the LINQ (Language Integrated Query) library provides a powerful and easy-to-use way to sort lists of objects based on one or more criteria. In this tutorial, we will demonstrate how to sort a list of Employee objects based on their salary using LINQ.

Syntax

Following is the syntax for sorting a list using LINQ OrderBy methods

// Sort in ascending order
var sortedList = list.OrderBy(item => item.Property);

// Sort in descending order  
var sortedList = list.OrderByDescending(item => item.Property);

Steps

  • Create an Employee class with properties for Name, Salary, and Department.

  • Create a List of Employee objects and populate it with some data.

  • Use LINQ to sort the list of Employee objects based on their salary.

  • Display the sorted list of Employee objects.

LINQ Sorting Process Original List John: 50000 Mary: 60000 Bob: 40000 OrderByDescending e => e.Salary Sorted List Alice: 70000 Mary: 60000 John: 50000 LINQ sorts employees by salary in descending order

Using OrderByDescending for Salary Sorting

Example

Here's the C# code for the example program that uses LINQ to sort a list of employees based on their salary

using System;
using System.Collections.Generic;
using System.Linq;

public class Employee {
   public string Name { get; set; }
   public int Salary { get; set; }
   public string Department { get; set; }
}

class Program {
   static void Main(string[] args) {
      List<Employee> employees = new List<Employee> {
         new Employee { Name = "John", Salary = 50000, Department = "ABC" },
         new Employee { Name = "Mary", Salary = 60000, Department = "DEF" },
         new Employee { Name = "Bob", Salary = 40000, Department = "XYZ" },
         new Employee { Name = "Alice", Salary = 70000, Department = "XYZ" }
      };
   
      var sortedEmployees = employees
         .OrderByDescending(e => e.Salary);
   
      Console.WriteLine("Employees sorted by salary (highest to lowest):");
      foreach (var employee in sortedEmployees) {
         Console.WriteLine($"{employee.Name}: ${employee.Salary:N0}");
      }
   }
}

The output of the above code is

Employees sorted by salary (highest to lowest):
Alice: $70,000
Mary: $60,000
John: $50,000
Bob: $40,000

Using OrderBy for Ascending Order

Example

using System;
using System.Collections.Generic;
using System.Linq;

public class Employee {
   public string Name { get; set; }
   public int Salary { get; set; }
   public string Department { get; set; }
}

class Program {
   static void Main(string[] args) {
      List<Employee> employees = new List<Employee> {
         new Employee { Name = "John", Salary = 50000, Department = "IT" },
         new Employee { Name = "Mary", Salary = 60000, Department = "HR" },
         new Employee { Name = "Bob", Salary = 40000, Department = "IT" },
         new Employee { Name = "Alice", Salary = 70000, Department = "Finance" }
      };
   
      var sortedEmployees = employees.OrderBy(e => e.Salary);
   
      Console.WriteLine("Employees sorted by salary (lowest to highest):");
      foreach (var employee in sortedEmployees) {
         Console.WriteLine($"{employee.Name}: ${employee.Salary:N0} ({employee.Department})");
      }
   }
}

The output of the above code is

Employees sorted by salary (lowest to highest):
Bob: $40,000 (IT)
John: $50,000 (IT)
Mary: $60,000 (HR)
Alice: $70,000 (Finance)

Multiple Criteria Sorting

Example

using System;
using System.Collections.Generic;
using System.Linq;

public class Employee {
   public string Name { get; set; }
   public int Salary { get; set; }
   public string Department { get; set; }
}

class Program {
   static void Main(string[] args) {
      List<Employee> employees = new List<Employee> {
         new Employee { Name = "John", Salary = 50000, Department = "IT" },
         new Employee { Name = "Mary", Salary = 50000, Department = "HR" },
         new Employee { Name = "Bob", Salary = 40000, Department = "IT" },
         new Employee { Name = "Alice", Salary = 70000, Department = "IT" }
      };
   
      var sortedEmployees = employees
         .OrderByDescending(e => e.Salary)
         .ThenBy(e => e.Name);
   
      Console.WriteLine("Sorted by salary (desc), then by name (asc):");
      foreach (var employee in sortedEmployees) {
         Console.WriteLine($"{employee.Name}: ${employee.Salary:N0} ({employee.Department})");
      }
   }
}

The output of the above code is

Sorted by salary (desc), then by name (asc):
Alice: $70,000 (IT)
John: $50,000 (IT)
Mary: $50,000 (HR)
Bob: $40,000 (IT)

How It Works

In these programs, we first create an Employee class with properties for Name, Salary, and Department. We then create a List of Employee objects and populate it with some sample data.

To sort the list of employees by salary, we use LINQ's OrderBy and OrderByDescending methods. These methods take a lambda expression that specifies the property to sort by (in this case, the Salary property). For multiple criteria sorting, we chain ThenBy or ThenByDescending methods.

The LINQ methods return an IOrderedEnumerable which maintains the sorting order when we iterate through it using a foreach loop.

Common LINQ Sorting Methods

Method Description
OrderBy() Sorts elements in ascending order by the specified key.
OrderByDescending() Sorts elements in descending order by the specified key.
ThenBy() Performs a secondary sort in ascending order.
ThenByDescending() Performs a secondary sort in descending order.

Conclusion

LINQ provides a simple and elegant way to sort lists of objects in C#. Using OrderBy and OrderByDescending methods with lambda expressions, you can easily sort collections based on any property. For complex sorting requirements, chain multiple sorting methods using ThenBy and ThenByDescending.

Updated on: 2026-03-17T07:04:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements