
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to use LINQ to sort a list in C#?
Use the LINQ orderby keyword to sort a list in C#.
In the below example, we have set the orderby for the elements −
var myLen = from element in myList orderby element.Length select element;
Let us see an example −
Example
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("cab"); myList.Add("motorbike"); var myLen = from element in myList orderby element.Length select element; foreach (string str in myLen) { Console.WriteLine(str); } } }
The above will produce the following result. The word with less number of characters will be displayed first like an ascending order list −
Output
bus cab truck motorbike
- Related Articles
- How to use LINQ in C#?
- How to flatten a list using LINQ C#?
- How to use “not in” query with C# LINQ?
- How to sort a list in C#?
- How to use std::sort to sort an array in C++
- C# Program to Find Integer Numbers from the List of Objects and Sort them Using LINQ
- How to make use of Join with LINQ and Lambda in C#?
- How to make use of both Take and Skip operator together in LINQ C#?
- How to make use of both Take and Skip operator together in LINQ C# Programming
- How to sort a list of strings in Python?
- How to sort a list of dictionaries by values of dictionaries in C#?
- How to sort a list of complex types using Comparison delegate in C#?
- How to sort the objects in a list in Python?
- How to use the Sort() method of array class in C#?
- How to sort a Python date string list?

Advertisements