Created
October 29, 2010 00:51
-
-
Save trbngr/652668 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ConsoleApplication3 | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var persons = new List<Person> | |
{ | |
new Person("Chris", 36), | |
new Person("Misty", 34), | |
new Person("Hailey", 5) | |
}; | |
var orderByAge = persons.OrderBy(p=>p.Age); | |
Console.Out.WriteLine("ordered by age"); | |
Console.Out.WriteLine("--------------"); | |
foreach (var person in orderByAge) | |
{ | |
Console.Out.WriteLine(person); | |
} | |
var orderedByName = persons.OrderBy(p=>p.Name); | |
Console.Out.WriteLine(""); | |
Console.Out.WriteLine("ordered by name"); | |
Console.Out.WriteLine("---------------"); | |
foreach (var person in orderedByName) | |
{ | |
Console.Out.WriteLine(person); | |
} | |
Console.Out.WriteLine(""); | |
Console.Out.WriteLine("press any key to exit."); | |
Console.ReadKey(true); | |
} | |
} | |
public class Person | |
{ | |
public Person(string name, int age) | |
{ | |
Name = name; | |
Age = age; | |
} | |
public string Name { get; private set; } | |
public int Age { get; private set; } | |
public override string ToString() | |
{ | |
return string.Format("{0} is {1} years old.", Name, Age); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment