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$
5
  • $\begingroup$ Tiebreaking is a term that just came to mind, but searching it yields no results. $\endgroup$ Commented Mar 19, 2015 at 6:29
  • 1
    $\begingroup$ Look at OrderedQ... $\endgroup$ Commented Mar 19, 2015 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$ Commented Mar 19, 2015 at 6:38
  • $\begingroup$ It's been a long day. Thanks, I'll fix. $\endgroup$ Commented Mar 19, 2015 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$ Commented Mar 19, 2015 at 12:21

2 Answers 2

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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.