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
Solved

Sort object using parameter for property name

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Set value of dynamically created checkboxlist 2 42
Expression Evaluater 3 37
VB.NET 2008 - SQL Timeout 9 34
Release Dynamically Allocated Memory in C# 3 38
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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