C# orderby Keyword

The orderby keyword in C# is used to sort data in LINQ queries. It allows you to sort collections in either ascending or descending order based on one or more criteria.

Syntax

Following is the syntax for using orderby in LINQ queries −

from element in collection
orderby element ascending
select element;

For descending order −

from element in collection
orderby element descending
select element;

Multiple sorting criteria can be applied using commas −

from element in collection
orderby element.Property1, element.Property2 descending
select element;

Using orderby for Ascending and Descending Order

The following example demonstrates sorting a list of strings by their length in both ascending and descending order −

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

class Demo {
   static void Main() {
      List<string> myList = new List<string>();
      myList.Add("truck");
      myList.Add("bus");
      myList.Add("bicycle");
      myList.Add("motorbike");
      
      var myLen = from element in myList
      orderby element.Length
      select element;
      
      Console.WriteLine("Ascending order...");
      foreach (string str in myLen){
         Console.WriteLine(str);
      }
      
      myLen = from element in myList
      orderby element.Length descending
      select element;
      
      Console.WriteLine("Descending order...");
      foreach (string str in myLen) {
         Console.WriteLine(str);
      }
   }
}

The output of the above code is −

Ascending order...
bus
truck
bicycle
motorbike
Descending order...
motorbike
bicycle
truck
bus

Using orderby with Alphabetical Sorting

The orderby keyword can also sort strings alphabetically −

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

class Program {
   static void Main() {
      List<string> fruits = new List<string> {"banana", "apple", "cherry", "date"};
      
      var sortedFruits = from fruit in fruits
      orderby fruit
      select fruit;
      
      Console.WriteLine("Alphabetical order:");
      foreach (string fruit in sortedFruits) {
         Console.WriteLine(fruit);
      }
   }
}

The output of the above code is −

Alphabetical order:
apple
banana
cherry
date

Using orderby with Multiple Criteria

You can sort by multiple criteria using commas. The following example sorts by length first, then alphabetically −

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

class Program {
   static void Main() {
      List<string> words = new List<string> {"cat", "dog", "elephant", "ant", "bee"};
      
      var sortedWords = from word in words
      orderby word.Length, word
      select word;
      
      Console.WriteLine("Sorted by length, then alphabetically:");
      foreach (string word in sortedWords) {
         Console.WriteLine($"{word} (Length: {word.Length})");
      }
   }
}

The output of the above code is −

Sorted by length, then alphabetically:
ant (Length: 3)
bee (Length: 3)
cat (Length: 3)
dog (Length: 3)
elephant (Length: 8)

Key Rules

  • The default sorting order is ascending. Use descending keyword for reverse order.

  • orderby can sort by any property or expression that returns a comparable value.

  • Multiple sorting criteria are evaluated from left to right in the order specified.

  • The orderby clause must come before the select clause in LINQ queries.

Conclusion

The orderby keyword in C# provides a simple way to sort data in LINQ queries. It supports both ascending and descending order, alphabetical sorting, and multiple sorting criteria, making it versatile for various data ordering requirements.

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

251 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements