![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements