user2962453
user2962453

Reputation: 143

Sorting specific List by specific value

I have a List which contains my-self made class elements. Here is my code:

{
    public partial class BestResultsScreen : Form
        {
        List<Result> my_results;
        public BestResultsScreen()
        {
            InitializeComponent();
            my_results = new List<Result>();
            Result r1 = new Result();
            r1.Name = "John";
            r1.Points = 158;
            r1.Year = 2013;
            my_results.Add(r1);
            Result r2 = new Result();
            r2.Name = "Mia";
            r2.Points = 253;
            r2.Year = 2014;
            my_results.Add(r2);
        }
    }
    class Result
    {
        public string Name;
        public int Points;
        public int Year;
    }
}

Can i sort this list, for example, by points? Thank You!

Upvotes: 1

Views: 84

Answers (1)

Kevin Brechb&#252;hl
Kevin Brechb&#252;hl

Reputation: 4727

You can do this with LINQ and the OrderBy() extension:

my_result.OrderBy(result => result.Points);

Upvotes: 3

Related Questions