Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
function void sortDynamicData(typeOfClass,SortKey)
{
List<dynamic> results = null; //results might be of any type it may contain students data or books data or professors data, hence I took as dynamic
results = services.GetMyresults(typeOfClass); //returns list of dynamic objects

results = results.OrderBy(SortKey).ToList();
 ...
 ...
 ...
 
}


my question is I want to sort the results based on sortKey
Any help would be greatly appreciable.
Posted
Updated 28-Dec-14 18:42pm
v3

Yes, this can be done using CompareTo().

Following links have already described this. Check if they help you

http://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value[^]
http://www.dotnetperls.com/sort-keyvaluepair[^]
http://stackoverflow.com/questions/14544953/c-sharp-sorting-a-listkeyvaluepairint-string[^]

Hopefully, this will resolve your problem :)
 
Share this answer
 
you can use LINQ for it
And check this url.
http://stackoverflow.com/questions/5295971/sorting-a-list-with-orderby[^]
 
Share this answer
 
C#
results = results.OrderByDescending(x =>
                    {
                        var dic = x as IDictionary<string, object>;
                        return dic[SortKey];
                    }).ToList();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900