
- 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
C# orderby Keyword
Use the orderby leyword to sort a list in ascending or descending order.
The following is the list −
List<string> myList = new List<string>(); myList.Add("truck"); myList.Add("bus"); myList.Add("bicycle"); myList.Add("motorbike");
Now let us sort the list in descending order −
myLen = from element in myList orderby element.Length descending select element;
Here is the complete code −
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("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); } } }
Output
Ascending order... bus truck bicycle motorbike Descending order... motorbike bicycle truck bus
- Related Articles
- C# Orderby Descending
- Orderby clause in C#
- ORDERBY word in MySQL?
- C# into keyword
- C++ mutable keyword?
- ‘this’ keyword in C#
- abstract keyword in C#
- static keyword in C#
- volatile keyword in C#
- Final keyword in C#
- try keyword in C#
- return keyword in C#
- Static Keyword in C++
- “extern” keyword in C
- “register” keyword in C

Advertisements