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:




I would like to use something like


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


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

I hope the example is meaningful and not too simplistic.

  • $\begingroup$ 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. $\endgroup$
    – george2079
    Commented Nov 15, 2016 at 13:21
  • $\begingroup$ 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. $\endgroup$
    – Bernd
    Commented Nov 15, 2016 at 13:25
  • 1
    $\begingroup$ This is just not what functions like Refine[] and Assuming[] are for, hence that Refine[TrueQ[a > b], a > b] returns False. $\endgroup$
    – Feyre
    Commented Nov 15, 2016 at 14:58

1 Answer 1

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


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


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} ,
   TrueQ[ Sort[{##}] == {a, b} ], OrderedQ[Reverse@{##}],
   True, OrderedQ[{##}]] &]

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

again many potential inconsistencies.

  • $\begingroup$ 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... $\endgroup$
    – Bernd
    Commented Nov 16, 2016 at 8:18
  • $\begingroup$ 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. $\endgroup$
    – george2079
    Commented Nov 16, 2016 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.