I’ve got a report being built from a dataset. The dataset uses the Sort property to order the data. I know that I can create a sort expression like this:
‘field desc, field2 asc’
But what I need now is a way to do a custom sort. In SQL, I can perform a custom sort by doing something like this:
order by case when field = 'Some Value' then 0 end case when field = 'Another Value' then 1 end
To basically re-define my sort (i.e, Some Value comes before Another Value).
Is it possible to do something similar as a sort expression against a DataView?
Ok, I just whipped this up real quick, and didn’t do all the neccessary error handling and null checking, but it should give you an idea and should be enough to get you started:
Usage:
//Sort by StringValue:
Result:
11 Eleven
14 Fourteen
10 Ten
13 Thirteen
12 Twelve
//Sort by IntValue:
Result:
10 Ten
11 Eleven
13 Thirteen
12 Twelve
14 Fourteen
EDIT: Changed it to extension method.
Now in your Lambda, (or you can create a full blown Comparison method) you can do any kind of custom sorting logic that you need. Remember, -1 is less than, 0 is equal to, and 1 is greater than.