As the title states - does a unique order exist for any Mathematica list to be sorted by the Sort[]
function (or as returned by a function that treats lists as sets)?
While I'm pretty sure the answer is yes, the kinds of elements you can throw into a Mathematica list seems so general and varied (numbers, strings, symbols, images, graphs, other lists, etc., etc.) that I thought to make sure.
(The reason this question occurred to me is an exercise question asked in the book I just started learning Mathematica from: Write a function SubsetQ[list1, list2]
that checks whether list1
is a subset of list2
. My solution is
SubsetQ[lis1_, lis2_] := Intersection[lis1, lis2] == Union[lis1, {}]
(* thanks to Simon Woods' correction, and assuming Intersection and Union sort canonically *)
but that implicitly assumes that the two lists on either side of the equality check will be sorted in the same order.)
SubsetQ[]
..." - you've already seenMemberQ[]
? $\endgroup$Intersection
must deal with general lists, which implies that its sorting function should be one that can sort any expression type. And since canonical sorting is such (and I don't think any other sorting is used generally in Mathematica) I would presumeIntersection
uses canonical sorting. To prove it would require someone to test all the specifications of the standard order linked by andre above. So in short: no, I don't know how to quickly testIntersection
's sorting order :) $\endgroup$Union
,Intersection
andComplement
. As a side note, you could avoid this problem in this particular case by using something likeSubsetQ[lis1_, lis2_] := Complement[list1, list2] === {}
. $\endgroup$