Generic List Class Sort Method

public void Sort(Comparison<T> comparison)
http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx

List<Person> pList = new List<Person>();
pList.Sort(ComparePersonByID); // pList를 ID로 정렬하기

[…]
public class Person : IComparable<Person>
{
public static Comparison<Person> ComparePersonByID =
delegate(Person p1, Person p2)
{
return p1.ID.CompareTo(p2.ID);
};

[…]
}

pList.Sort(ComparePersonByID); // pList를 ID로 정렬하기 (Comparison delegate)

public class Person : IComparable<Person>
{
public static Comparison<Person> ComparePersonByID =
delegate(Person p1, Person p2)
{
return p1.ID.CompareTo(p2.ID);
};

[…]
}

pList.Sort(new PersonIDComparer()); // pList를 ID로 정렬하기 (IComparer)

public class PersonIDComparer : IComparer<Person>
{
public int Compare(Person p1, Person p2)
{
return p1.ID.CompareTo(p2.ID);
}
}

pList.Sort((p, q) => p.ID.CompareTo(q.ID)); // pList를 ID로 정렬하기 (lambda)

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *