Edit: Changed Code to closely match your syntax
This code sorts on the client, but works with all IEnumerables
. If you absolutely need to sort on the database, take a look at Yacoub's static MyClass()
to see how he solved this problem.
The example below is based on the information you provided, you might need to adjust it a bit.
public class DemoClass
{
public DateTime Created { get; set; }
public bool Approved { get; set; }
public Person Author { get; set; }
}
public class Person
{
public string Name { get; set; }
}
Since your example contains author
which actually resolves to Author.Name
, you need to create some sort of mapping for your keywords (Like you did with your OrderExpression
class).
public class OrderExpressions<T>
{
private readonly Dictionary<string,Func<T,object>> _mappings =
new Dictionary<string,Func<T, object>>();
public OrderExpressions<T> Add(Func<T, object> expression, string keyword)
{
_mappings.Add(keyword, expression);
return this;
}
public Func<T, object> this[string keyword]
{
get { return _mappings[keyword]; }
}
}
Which could be used like this:
OrderExpressions<DemoClass> expressions = new OrderExpressions<DemoClass>()
.Add(x => x.Created, "created")
.Add(x => x.Approved, "approved")
.Add(x => x.Author.Name, "author");
You can pass those functions / lambda expressions, directly to Linq and add the next comparison one after another. Start with OrderBy
or OrderByDescrending
, that will give you your first IOrderedEnumerable
and then add all remaining arguments with ThenBy
or ThenByDescending
.
public static class KeywordSearchExtender
{
public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> data,
OrderExpressions<T> mapper, params string[] arguments)
{
if (arguments.Length == 0)
throw new ArgumentException(@"You need at least one argument!", "arguments");
List<SortArgument> sorting = arguments.Select(a => new SortArgument(a)).ToList();
IOrderedEnumerable<T> result = null;
for (int i = 0; i < sorting.Count; i++)
{
SortArgument sort = sorting[i];
Func<T, object> lambda = mapper[sort.Keyword];
if (i == 0)
result = sorting[i].Ascending ?
data.OrderBy(lambda) :
data.OrderByDescending(lambda);
else
result = sorting[i].Ascending ?
result.ThenBy(lambda) :
result.ThenByDescending(lambda);
}
return result;
}
}
public class SortArgument
{
public SortArgument()
{ }
public SortArgument(string term)
{
if (term.StartsWith("-"))
{
Ascending = false;
Keyword = term.Substring(1);
}
else if (term.StartsWith("+"))
{
Ascending = true;
Keyword = term.Substring(1);
}
else
{
Ascending = true;
Keyword = term;
}
}
public string Keyword { get; set; }
public bool Ascending { get; set; }
}
All together it be used like this:
var data = WhateverYouDoToGetYourData();
var expressions = new OrderExpressions<DemoClass>()
.Add(x => x.Created, "created")
.Add(x => x.Approved, "approved")
.Add(x =>x.Author.Name, "author");
var result = data.OrderBy(expressions, "+created", "-approved", "+author");
// OR
var result = data.OrderBy(expressions, fields);
You can find my proof-of-concept on dotNetFiddle.