Evaluate function using assumptions

I know there is an option Assumptions for functions such as Simplify, Integrate, etc. I am trying to use assumptions also for other functions. A simplified example:

Sort[{a,b}]


yields

{a,b}

I would like to use something like

Refine[Sort[{a,b}],a>b]


(which still produces {a,b}) to yield

{b,a}

but am unable to achieve this, even when playing around with Unevaluated, Hold, etc.

I hope the example is meaningful and not too simplistic.

• you need to supply Sort with an ordering function as a second argument. With just that example its not clear what the rule should be. – george2079 Nov 15 '16 at 13:21
• But even if I use e.g. Sort[{a, b}, Less], the problem persists. The "rule" in this case is simply supposed to indicate that b is less than a and not vice versa, i.e. that it should be sorted {b,a}. @Kuba: I'm up for more complex solutions with potential Indeterminate results of the rules are inconsistent, but I could not achieve anything like this. – Bernd Nov 15 '16 at 13:25
• This is just not what functions like Refine[] and Assuming[] are for, hence that Refine[TrueQ[a > b], a > b] returns False. – Feyre Nov 15 '16 at 14:58

rank = {a -> 2, b -> 1};
SortBy[{a, b}, # /. rank &]


{b,a}

this will have all sorts of odd behavior unless you assign a rank to everything in the input list.

similar:

desiredorder = {c, b, a};
SortBy[{a, b, c, a, c}, Position[desiredorder, #] &]


{c, c, b, a, a}

You might try something like this,

Sort[ {1, a, 0, b, 3, c} ,
Which[
TrueQ[ Sort[{##}] == {a, b} ], OrderedQ[Reverse@{##}],
True, OrderedQ[{##}]] &]


{0, 1, 3, b, a, c}

again many potential inconsistencies.

• Thank you! But that would mean in a more complex example, I would first have to translate all my assumptions into some "rank set"? So I should rather not do what I am trying to do...? I was surprised it is such that an odd wish... – Bernd Nov 16 '16 at 8:18
• one way or another you need rules for every pair of items in the list that results in a unique ordering regardless of the order that the sort algorithm makes the comparisons. – george2079 Nov 16 '16 at 12:37