3
$\begingroup$

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.

$\endgroup$
  • $\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 Nov 15 '16 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 Nov 15 '16 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 Nov 15 '16 at 14:58
3
$\begingroup$
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.

$\endgroup$
  • $\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 Nov 16 '16 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 Nov 16 '16 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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