Solved

Sort object using parameter for property name

Posted on 2013-01-24
2
333 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
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 500 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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