I have to Sort a List of Class
I have 3 Properties in my Class (EX ID,Name,Age)
In my List i have 10 objects of <Class> added
I need to sort By Id or By Name or by Age like DataView Sort.is it possible ?
Please enligten me

Thanx,
How to SORT a System.Collections.Generic.List

I have to Sort a List of Class
I have 3 Properties in my Class (EX ID,Name,Age)
In my List i have 10 objects of <Class> added
I need to sort By Id or By Name or by Age like DataView Sort.is it possible ?
Please enligten me

Thanx,

——————

There is no flexible way to sort a List by any property on it’s containing type, but you can easily create custom sorting logic, you just need to write different code for each sorting scenario. Here is a quick and dirty example:

List<Class> classList = new List<Class>();

classList.Add(new Class(24, “Jon”, 12));
classList.Add(new Class(16, “Mike”, 37));
classList.Add(new Class(48, “Peter”, 31));

// Sort by ID.
classList.Sort(delegate(Class class1, Class class2)
{
return (Comparer<int>.Default.Compare(class1.ID, class2.ID));
});

// Sort by name.
classList.Sort(delegate(Class class1, Class class2)
{
return (Comparer<string>.Default.Compare(class1.Name, class2.Name));
});

// Sort by age.
classList.Sort(delegate(Class class1, Class class2)
{
return (Comparer<int>.Default.Compare(class1.Age, class2.Age));
});