5
$\begingroup$

Like multi-column sorting. For example,

{1,3,5} > {1,2,5}

would return True, while

{1,3,5} > {1,5,2}

would return False.

I'm betting there's a simple term for this and that term would lead straight to some function, but I can't think of the term. It'd be a trivial function to write, but there must be something built-in...

(I'm looking for a predicate I can supply to this PriorityQueue implementation.)

$\endgroup$
  • $\begingroup$ Tiebreaking is a term that just came to mind, but searching it yields no results. $\endgroup$ – Andrew Cheong Mar 19 '15 at 6:29
  • 1
    $\begingroup$ Look at OrderedQ... $\endgroup$ – ciao Mar 19 '15 at 6:35
  • $\begingroup$ Agh, that was what I was looking for. Still not well-versed in $Mathematica$. I didn't think to read through the *Q functions, even after thinking the word "Predicate"—I guess I'd only seen more primitive *Q functions so didn't think I'd find my answer there. In case this question might help someone else searching the terms I've used, could you post your comment as an answer? Thanks. $\endgroup$ – Andrew Cheong Mar 19 '15 at 6:38
  • $\begingroup$ It's been a long day. Thanks, I'll fix. $\endgroup$ – Andrew Cheong Mar 19 '15 at 8:07
  • $\begingroup$ what about {1,3,5} > {1,2,7}? You could make a case for either answer. Or {1,3,5} > {1,1,3,5} ? $\endgroup$ – ControlAltDel Mar 19 '15 at 12:21
4
$\begingroup$

I propose using Order, assuming equal-length lists.

Order[{1, 3, 5}, {1, 3, 4}]
Order[{1, 3, 5}, {1, 5, 2}]
Order[{1, 3, 5}, {1, 3, 5}]
-1

1

0

You can assign an infix operator if you wish:

CirclePlus = Order;

{1, 3, 5} ⊕ {1, 3, 4}
{1, 3, 5} ⊕ {1, 5, 2}
{1, 3, 5} ⊕ {1, 3, 5}
-1

1

0

You can convert the numeric output to Boolean as needed, but often it is faster to use it numerically. An example of conversion:

Star = Composition[Negative, Order];

{1, 3, 5} ⋆ {1, 3, 4}
{1, 3, 5} ⋆ {1, 5, 2}
{1, 3, 5} ⋆ {1, 3, 5}
True

False

False
$\endgroup$
3
$\begingroup$
listGreater[l1_, l2_] := OrderedQ@{l2, l1}&& l1 =!= l2
listLess[l1_, l2_] := OrderedQ@{l1, l2}&& l1 =!= l2
listGreaterEqual[l1_, l2_] := OrderedQ@{l2, l1}
listLessEqual[l1_, l2_] := OrderedQ@{l1, l2}

{1, 3, 5}~listGreater~{1, 3, 4}

(* True *)
$\endgroup$

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.