We use OrderBy to arrange items in a list from smallest to largest or in a specific order. It helps us find things quickly and see data clearly.
When you want to show a list of names in alphabetical order.
When you need to sort numbers from lowest to highest.
When you want to organize products by price from cheapest to most expensive.
When you want to display dates from earliest to latest.
When you want to sort students by their grades.
Syntax
C Sharp (C#)
var sortedList = list.OrderBy(item => item.Property);
OrderBy returns a new sorted sequence; it does not change the original list.
You can use OrderBy with any property or value you want to sort by.
Examples
This sorts a list of numbers from smallest to largest.
C Sharp (C#)
var numbers = newList<int> { 5, 3, 8, 1 };
var sortedNumbers = numbers.OrderBy(n => n);
This sorts a list of names alphabetically.
C Sharp (C#)
var names = newList<string> { "Bob", "Alice", "Eve" };
var sortedNames = names.OrderBy(name => name);
This sorts a list of people by their age from youngest to oldest.
C Sharp (C#)
var people = newList<Person> {
new Person { Name = "Bob", Age = 30 },
new Person { Name = "Alice", Age = 25 }
};
var sortedByAge = people.OrderBy(p => p.Age);
Sample Program
This program creates a list of people and sorts them by their names alphabetically using OrderBy. Then it prints each person's name and age.
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;
class Person
{
public string Name { get; set; }
publicint Age { get; set; }
}
class Program
{
staticvoid Main()
{
var people = newList<Person>
{
new Person { Name = "Charlie", Age = 35 },
new Person { Name = "Alice", Age = 25 },
new Person { Name = "Bob", Age = 30 }
};
var sortedPeople = people.OrderBy(p => p.Name);
foreach (var person in sortedPeople)
{
Console.WriteLine($"{person.Name} - {person.Age}");
}
}
}
OutputSuccess
Important Notes
OrderBy does not change the original list; it creates a new sorted sequence.
To sort in reverse order, use OrderByDescending instead.
You can chain OrderBy with ThenBy to sort by multiple properties.
Summary
OrderBy helps sort lists by a chosen property or value.
It returns a new sorted sequence without changing the original list.
Use OrderByDescending to sort from largest to smallest.
Practice
(1/5)
1. What does the OrderBy method do in C#?
easy
A. Sorts a collection in ascending order based on a key
B. Deletes elements from a list
C. Reverses the order of elements in a list
D. Filters elements based on a condition
Solution
Step 1: Understand the purpose of OrderBy
The OrderBy method sorts elements in a collection based on a key in ascending order.
Step 2: Compare with other options
Options B, C, and D describe different operations (deletion, reversing, filtering) which OrderBy does not perform.
Final Answer:
Sorts a collection in ascending order based on a key -> Option A
Quick Check:
OrderBy = Sort ascending [OK]
Hint: OrderBy sorts ascending by key, not filtering or deleting [OK]
Common Mistakes:
Confusing OrderBy with filtering methods like Where
Thinking OrderBy modifies the original list
Mixing up OrderBy with reversing or deleting
2. Which of the following is the correct syntax to sort a list of integers named numbers in ascending order using OrderBy?
easy
A. numbers.OrderBy();
B. numbers.OrderBy(n);
C. numbers.OrderBy(n => n);
D. numbers.OrderBy(n => );
Solution
Step 1: Check the correct lambda syntax
OrderBy requires a key selector function like n => n to specify sorting key.
Step 2: Validate each option
numbers.OrderBy(n => n); uses correct lambda syntax. numbers.OrderBy(); misses the key selector. numbers.OrderBy(n); passes a variable, not a lambda. numbers.OrderBy(n => ); has incomplete lambda syntax.
Final Answer:
numbers.OrderBy(n => n); -> Option C
Quick Check:
OrderBy needs a key selector lambda [OK]
Hint: OrderBy always needs a key selector lambda like n => n [OK]
Common Mistakes:
Omitting the lambda expression inside OrderBy
Passing a variable instead of a lambda
Using incomplete or invalid lambda syntax
3. What will be the output of the following code?
var fruits = new List<string> { "banana", "apple", "cherry" };
var sorted = fruits.OrderBy(f => f);
foreach(var fruit in sorted) {
Console.Write(fruit + " ");
}
medium
A. banana apple cherry
B. apple banana cherry
C. cherry banana apple
D. apple cherry banana
Solution
Step 1: Understand the sorting key
The code sorts the list of fruits alphabetically by their string value.
Step 2: Determine the sorted order
Alphabetically, "apple" comes before "banana", which comes before "cherry".