.Net Akademi eğitiminden bir ders çalışması...
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Collections;
namespace sortedbyProperty
{
class Program
{
static void Main(string[] args)
{
HedeColl h = new HedeColl();
h.Add(new Ogrenci(2, "ali", DateTime.Now.AddDays(10)));
h.Add(new Ogrenci(21, "ahmet", DateTime.Now.AddDays(1)));
h.Add(new Ogrenci(12, "mehmet", DateTime.Now.AddDays(122)));
h.Sortla("Tarih",BuyukKucuk.Buyuk);
foreach (Ogrenci o in h)
{
Console.WriteLine(o);
}
}
}
enum BuyukKucuk
{
Buyuk,
Kucuk
}
class HedeColl : List<Ogrenci>
{
public HedeColl()
{
}
public void Sortla(string pad, BuyukKucuk siralama)
{
this.Sort(
delegate(Ogrenci o1, Ogrenci o2)
{
PropertyInfo prop1 = o1.GetType().GetProperty(pad);
PropertyInfo prop2 = o2.GetType().GetProperty(pad);
object v1 = prop1.GetValue(o1, null);
object v2 = prop2.GetValue(o2, null);
int val = Comparer.Default.Compare(v1, v2);
if (siralama == BuyukKucuk.Kucuk)
return val;
else
return -val;
}
);
}
}
class Ogrenci
{
private int _no;
public int No
{
get { return _no; }
set { _no = value; }
}
private string _ad;
public string Ad
{
get { return _ad; }
set { _ad = value; }
}
private DateTime _tarih;
public DateTime Tarih
{
get { return _tarih; }
set { _tarih = value; }
}
public Ogrenci(int a, string b, DateTime d)
{
Ad = b;
No = a;
Tarih = d;
}
public override string ToString()
{
return String.Format("Ad :{0} No : {1} Tarih : {2}", Ad, No, Tarih);
}
}
}