Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace SortExample
- {
- public class Program
- {
- public static void Main()
- {
- List<Person> people = new List<Person>()
- {
- new Person {FirstName = "John", LastName = "Doe", Age = 33},
- new Person {FirstName = "Maragaret", LastName = "Jones", Age = 28},
- new Person {FirstName = "David", LastName = "Reyes", Age = 55},
- new Person {FirstName = "Andy", LastName = "Holmes", Age = 44}
- };
- // set List<Person> equal to the sorted list based on age
- people = people.OrderBy(p => p.Age).ToList();
- foreach(var p in people)
- {
- Console.WriteLine(p.FullName + " - " + p.Age);
- }
- }
- }
- public class Person
- {
- public string FirstName {get; set;}
- public string LastName {get; set;}
- public int Age {get; set;}
- public string FullName
- {
- get {return FirstName + " " + LastName;}
- }
- public Person() {}
- public Person(string firstName, string lastName, int age)
- {
- this.FirstName = firstName;
- this.LastName = lastName;
- this.Age = age;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement