C# Program to Sort a List of Employees Based on Salary using LINQ
Given a list of employees, now we sort the list of employees according to their salary using LINQ. So to this task, we use the OrderBy() method. This method is used to sort the elements of the specified sequence in ascending order.
Example:
Input: {id = 101, Name = Rohit, Salary = 50000, Department = HR} {id = 104, Name = Rohit, Salary = 10000, Department = Development} {id = 106, Name = Rohit, Salary = 80000, Department = HR} {id = 108, Name = Rohit, Salary = 20000, Department = Development} Output: {id = 104, Name = Rohit, Salary = 10000, Department = Development} {id = 108, Name = Rohit, Salary = 20000, Department = Development} {id = 101, Name = Rohit, Salary = 50000, Department = HR} {id = 106, Name = Rohit, Salary = 80000, Department = HR}
Approach:
1. Create a list of employees along with their id, name, salary, and department.
2. Now sort the employee’s list according to their salary using the OrderBy() method.
var result_set = Beginner.OrderBy(sal => sal.Emp_Salary);
Or we can also sort the list using the order OrderBy clause of LINQ
var result_set = from emp in Beginner orderby emp.Emp_Salary select emp;
3. Display the sorted list using foreach loop.
Example 1:
C#
// C# program to sort a list of employees // based on salary. Using OrderBy() method using System; using System.Linq; using System.Collections.Generic; class Geek{ int emp_id; string Emp_Name; int Emp_Salary; string Emp_Department; static void Main( string [] args) { // Beginner data List<Geek> Beginner = new List<Geek>() { new Geek{emp_id = 101, Emp_Name = "arjun" , Emp_Salary = 50000, Emp_Department = "ABC" }, new Geek{emp_id = 102, Emp_Name = "bheem" , Emp_Salary = 65000, Emp_Department = "DEF" }, new Geek{emp_id = 103, Emp_Name = "krishna" , Emp_Salary = 45000, Emp_Department = "ABC" }, new Geek{emp_id = 104, Emp_Name = "Ram" , Emp_Salary = 20000, Emp_Department = "DEF" }, new Geek{emp_id = 105, Emp_Name = "kiran" , Emp_Salary = 70000, Emp_Department = "DEF" }, new Geek{emp_id = 106, Emp_Name = "karna" , Emp_Salary = 50000, Emp_Department = "ABC" }, }; // We have sorted the data using OrderBy() command var result_set = Beginner.OrderBy(sal => sal.Emp_Salary); // Display the sorted result foreach (Geek emp in result_set) { Console.WriteLine(emp.emp_id + " " + emp.Emp_Name + " " + emp.Emp_Salary + " " + emp.Emp_Department); } } } |
Output
104 Ram 20000 DEF 103 krishna 45000 ABC 101 arjun 50000 ABC 106 karna 50000 ABC 102 bheem 65000 DEF 105 kiran 70000 DEF
Example 2:
C#
// C# program to sort a list of employees // based on salary. Using OrderBy() method using System; using System.Linq; using System.Collections.Generic; class Geek{ int emp_id; string Emp_Name; int Emp_Salary; string Emp_Department; static void Main( string [] args) { // Beginner data List<Geek> Beginner = new List<Geek>() { new Geek{emp_id = 201, Emp_Name = "Sumit" , Emp_Salary = 50000, Emp_Department = "ABC" }, new Geek{emp_id = 202, Emp_Name = "Rohan" , Emp_Salary = 65000, Emp_Department = "DEF" }, new Geek{emp_id = 203, Emp_Name = "Mohit" , Emp_Salary = 45000, Emp_Department = "ABC" }, new Geek{emp_id = 204, Emp_Name = "Sonam" , Emp_Salary = 20000, Emp_Department = "DEF" }, new Geek{emp_id = 205, Emp_Name = "Shive" , Emp_Salary = 70000, Emp_Department = "DEF" }, }; // We have sorted the data using OrderBy linq clause var result_set = from emp in Beginner orderby emp.Emp_Salary select emp; // Display the sorted result foreach (Geek emp in result_set) { Console.WriteLine(emp.emp_id + " " + emp.Emp_Name + " " + emp.Emp_Salary + " " + emp.Emp_Department); } } } |
Output
204 Sonam 20000 DEF 203 Mohit 45000 ABC 201 Sumit 50000 ABC 202 Rohan 65000 DEF 205 Shive 70000 DEF