2

Suppose I have this list of process or any other object

List<Process> listProcess = new List<Process>();

I can sort it using this line listProcess.OrderBy(p => p.Id); But what if I have only string name of property obtained in runtime. I assume, I should use reflection to get the property object. Can I use orderby method or I should use Sort and then pass own comparer?

1 Answers1

5

You can have a look at the post referred in the comment. Or, you can achieve that using simple reflection like this

var sortedList = list.OrderBy(o => o.GetType().GetProperty(propName).GetValue(o));

Where

List<object> list; //a list of any object(s)
string propName; //name of the property to be used in OrderBy
Arghya C
  • 9,805
  • 2
  • 47
  • 66