Skip to content

Instantly share code, notes, and snippets.

@trbngr
Created October 29, 2010 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trbngr/652668 to your computer and use it in GitHub Desktop.
Save trbngr/652668 to your computer and use it in GitHub Desktop.
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