var simpleList = new List<ObjectName>();
// Order by a property
list.OrderBy(o => o.PropertyName);
// Order by multiple property
list.OrderBy(o => o.PropertyName).ThenBy(o => o.AnotherProperty);
// Same but descending
list.OrderByDescending(o => o.PropertyName).ThenByDescending(o => o.AnotherProperty);
public class People
{
public string FirstName;
public String LastName;
public Person(string firstName,string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public static void Main(string args[])
{
Person[] people = {
new Person(){ FirstName="Steve", LastName="Jobs"},
new Person(){ FirstName="Bill", LastName="Gates"},
new Person(){ FirstName="Lary", LastName="Page"}
};
Array.Sort(people);
}
}
var users = new List<User>();
users.Add(new User("John", "Doe", 1230));
users.Add(new User("Lucy", "Novak", 670));
users.Add(new User("Ben", "Walter", 2050));
users.Add(new User("Robin", "Brown", 2300));
users.Add(new User("Amy", "Doe", 1250));
users.Add(new User("Joe", "Draker", 1190));
users.Add(new User("Janet", "Doe", 980));
users.Add(new User("Albert", "Novak", 1930));
users.Sort((u1, u2) =>
{
int result = u1.LastName.CompareTo(u2.LastName);
return result == 0 ? u1.Salary.CompareTo(u2.Salary) : result;
});