?
Solved

Sort object using parameter for property name

Posted on 2013-01-24
2
Medium Priority
?
341 Views
Last Modified: 2013-01-24
This seems to work it sorts my list of ProjectActions by DueDate

actionList.Sort(delegate(ProjectAction al1, ProjectAction al2) { return al1.DueDate.CompareTo(al2.DueDate); });

I would like to pass in the property to sort by as a parameter

string SortBy = "DueDate"

something like this.
al1(SortBy).compareto al2(SortBy)

Is it possible to do this
0
Comment
Question by:Eamon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38813824
you don't need to pass the property as string, use OrderBy:

actionList.OrderBy(n=>n.DueDate);

Open in new window


you can also sort by multiple properties, for instance:

actionList.OrderBy(n => n.DueDate).ThenBy(n =>n.Property2).ThenBy(n => n.Property3);

Open in new window

0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 2000 total points
ID: 38814051
It would be possible, but very complicated because you would need to use reflection to get the properties, and still have different code for different data types.

Create a delegate depending on the parameter name. Example:

Func<ProjectAction, ProjectAction, bool> compare;
switch (SortBy) {
  case "Name": compare = (x, y) => x.Name.CompareTo(y.Name); break;
  case "DueDate": compare = (x, y) => x.DueDate.CompareTo(y.DueDate); break;
  default: throw new NotImplementedException();
}

Open in new window

Then sort the list using the delegate:

actionList.Sort(compare);

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question