Lesson 14/31

Tutorials LINQ Mastery

OrderBy & OrderByDescending: Sorting data

On this page

Sorting the Stream

Sorting is a fundamental operation. In LINQ, OrderBy provides a clean, fluent way to arrange your data.

1. Basic Sorting

OrderBy sorts in ascending order (A-Z, 0-9). OrderByDescending does the opposite. **Architect Note:** Sorting is an O(N log N) operation. If you are sorting a remote database via IQueryable, ensure the column you are sorting by is **Indexed** in the DB, or your performance will tank as the table grows.

2. Custom Comparers

You can pass a custom IComparer<T> to handle complex sorting logic (e.g., case-insensitive strings or natural sorting for files). Most of the time, the default comparer is sufficient.

3. Architect Insight

Q: "Should I sort before or after filtering?"

Architect Answer: "Always filter (Where) BEFORE you sort. Sorting 1,000 items is significantly faster than sorting 1,000,000 items. By reducing the data set first, you minimize the work the sorting algorithm has to perform."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

LINQ Mastery
Course syllabus
General
1. Core Foundations
2. Filtering & Transformation
3. Aggregation & Quantifiers
4. Ordering & Partitioning
5. Sets & Lookups
6. Join & Grouping
7. Advanced Providers & Parallelism
8. Real-world Performance & Patterns
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details