I'm trying to sort a List<T>
, without using OrderBy
, OrderByDescending
, where T
is a custom class.
Code:
class Something
{
public string Category { get; set; }
public int Fingers { get; set; }
public DateTime Creation { get; set; }
}
The list order it's based on any property of T
.
class BigRoom
{
var Room = new Room(new List<Something>());
}
class Room<T> where T: class, new()
{
List<T> baseList;
public Room(List<T> listPar)
{
baseList = listPar;
var prop = /* get any property from T with reflection... */
// How to set a comparer here, if we know prop (type, value...)
baseList.Sort(...);
// go do something with reordered list
}
}
I can do it knowing T
and its properties, using lambda expressions or delegates.
list.Sort((x, y) => x.CompareTo(y));
But when getting prop
values, it returns an object
, which it doesn't implement CompareTo()
, is there any way of achieving this, if so I'll be grateful.