Click here to Skip to main content
14,743,899 members
Articles » Languages » C# » How To
Tip/Trick
Posted 17 Apr 2014

Tagged as

Stats

163.9K views
5 bookmarked

How to Sort a List (C#)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (13 votes)
18 Apr 2014CPOL
Tips on sorting a list of objects.

Take this class as example.

public class Player
{
    public string Name { get; set; }
    public int Total { get; set; }

    public Player(string name, int total)
    {
        Name = name;
        Total = total;
    }
}

Creating the list.

List<Player> lst = new List<player>();
lst.Add(new Player("John", 100));
lst.Add(new Player("Smith", 120));
lst.Add(new Player("Cait", 97));
lst.Add(new Player("Irene", 100));
lst.Add(new Player("Ben", 100)); 
lst.Add(new Player("Deniel", 88));

Sort By Using List<t>.Sort() Method

Sort by single element "Total" in ascending order:

lst.Sort(delegate(Player x, Player y)
{
    return x.Total.CompareTo(y.Total);
});

Sort by single element "Total" in descending order:

lst.Sort(delegate(Player x, Player y)
{
    return y.Total.CompareTo(x.Total);
});

Sort by multiple elements:

lst.Sort(delegate(Player x, Player y)
{
    // Sort by total in descending order
    int a = y.Total.CompareTo(x.Total);

    // Both player has the same total.
    // Sort by name in ascending order
    if (a == 0)
        a = x.Name.CompareTo(y.Name);

    return a;
});

Sort By Using Linq

Sorting by using Linq requires less syntax.

var result1 = lst.OrderBy(a => a.Total);

var result3 = lst.OrderBy(a => a.Total).ThenBy(a => a.Name);

var result2 = lst.OrderByDescending(a => a.Total).ThenByDescending(a => a.Name);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

adriancs
Software Developer
Malaysia Malaysia
Programming is an art.

Comments and Discussions

 
QuestionThanks for sharing Easy and quick solution for any type collection sorting Pin
Mr.Rajendra Taradale4-Dec-17 20:48
MemberMr.Rajendra Taradale4-Dec-17 20:48 
GeneralMy vote of 4 Pin
Peter Huber SG14-Jul-15 0:00
MemberPeter Huber SG14-Jul-15 0:00 
GeneralMy vote of 2 Pin
LostTheMarbles22-Apr-14 5:24
professionalLostTheMarbles22-Apr-14 5:24 
QuestionSome more ways to sort . Pin
George Swan18-Apr-14 22:34
MemberGeorge Swan18-Apr-14 22:34 
GeneralMy vote of 2 Pin
Emre Ataseven18-Apr-14 3:17
professionalEmre Ataseven18-Apr-14 3:17 
GeneralRe: My vote of 2 Pin
SRKarn18-Apr-14 6:35
MemberSRKarn18-Apr-14 6:35 
GeneralMy vote of 1 Pin
giganoide18-Apr-14 0:10
Membergiganoide18-Apr-14 0:10 
GeneralRe: My vote of 1 Pin
adriancs18-Apr-14 1:10
professionaladriancs18-Apr-14 1:10 
GeneralMy vote of 1 Pin
mariuszkiler18-Apr-14 0:02
Membermariuszkiler18-Apr-14 0:02 
GeneralRe: My vote of 1 Pin
adriancs18-Apr-14 0:11
professionaladriancs18-Apr-14 0:11 
GeneralRe: My vote of 1 Pin
mariuszkiler18-Apr-14 0:15
Membermariuszkiler18-Apr-14 0:15 
GeneralRe: My vote of 1 Pin
adriancs18-Apr-14 1:10
professionaladriancs18-Apr-14 1:10 
GeneralRe: My vote of 1 Pin
HaBiX19-Apr-14 21:11
MemberHaBiX19-Apr-14 21:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.