Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

How to sort a List in C#

Date- Jun 01,2023

3795

In C#, you can use the Sort method to sort a List in ascending order. Here's an example of how you can sort a list of integers:

List<int> numbers = new List<int> { 4, 2, 7, 1, 9 };
numbers.Sort();

After executing the Sort method, the numbers list will be sorted in ascending order: { 1, 2, 4, 7, 9 }.

If you want to sort a list of objects based on a specific property, you can use the Sort method with a custom comparison. Here's an example where we sort a list of Person objects based on their Name property:

class Person
{
    public string Name { get; set; }
    // other properties
}

List<Person> people = new List<Person>
{
    new Person { Name = "Alice" },
    new Person { Name = "Charlie" },
    new Person { Name = "Bob" }
};

people.Sort((p1, p2) => p1.Name.CompareTo(p2.Name));

After executing the Sort method with the custom comparison, the people list will be sorted alphabetically by name: { "Alice", "Bob", "Charlie" }.

Alternatively, if you want to sort a list in descending order, you can use the Sort method with a custom comparison and reverse the result using the Reverse method:

List<int> numbers = new List<int> { 4, 2, 7, 1, 9 };
numbers.Sort((x, y) => y.CompareTo(x));
numbers.Reverse();
After executing these two methods, the numbers list will be sorted in descending order: { 9, 7, 4, 2, 1 }.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Zoom C# Wrapper Integration
Jun 12, 2021
Convert HTML String To Image In C#
Jul 02, 2022
The report definition is not valid or is not supported by this version of reporting
Jul 02, 2022
Replacing Accent Characters with Alphabet Characters in CSharp
Jun 03, 2023

Comments

Contents

More in C#

  • Get IP address using c# 8573 views
  • How to Convert DataTable to List 7593 views
  • Asynchronous Programming in C# 7532 views
  • How to Verify If base 64 string is valid for tiff image in C… 7099 views
  • Compress image using ImageCompress NuGet package 6911 views
View all C# posts →

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page