3
$\begingroup$

I have to order the following list according to the sum of the powers of each element of the list, using the function Sort.

mylist = Table[x^(i)^3 + 3 x^(i^2 - 6 i + 5) + i, {i, -6, 6}]

I can do it using Ordering:

mylist[[Ordering[Total[Exponent[mylist, x, List], {2}]]]]

but I don't know how to do it using Sort. All my attempts failed. Could someone help me?

$\endgroup$

2 Answers 2

4
$\begingroup$

This is more of an extended comment to the OP's question and halirutan's answer, although there is the extra solution below that uses SortBy rather than Sort, and I find the syntax of SortBy easier to understand than Sort. In any case, note the following:

sortFunc[expr_] := Total[Exponent[expr, x, List]];
order[0] = mylist[[Ordering[Total[Exponent[mylist, x, List], {2}]]]];
order[1] = Sort[mylist, sortFunc[#1] < sortFunc[#2] &];
order[2] = Sort[mylist, sortFunc[#1] <= sortFunc[#2] &];
order[3] = SortBy[mylist, sortFunc];
Outer[SameQ, #, #, 1, 1]&@Array[order, 4, 0]
(* { {True, False, True, False}
     , {False, True, False, False}
     , {True, False, True, False}
     , {False, False, False, True} } *)

In other words, three of these four ways of doing things return different results! halirutans way, using < gives a slightly different ordering than the OP's way, but changing < to <= gets back the OP's ordering.

Of course, this isn't entirely surprising:

sortFunc /@ mylist
(* {-139, -65, -19, 5, 13, 11, 5, 1, 5, 23, 61, 125, 221} *)

So there are three terms with the total of the exponents being five. Ordering keeps the order of the elements that are the same:

Flatten@Position[sortFunc /@ mylist, 5]
Ordering[sortFunc /@ mylist]
(* {4, 7, 9} *)
(* {1, 2, 3, 8, 4, 7, 9, 6, 5, 10, 11, 12, 13} *)

Note the 4, 7, 9 in Sequence inside the Ordering list. On the other hand, SortBy yields a different ordering:

Position[mylist, #] & /@ order[3] // Flatten
(* {1, 2, 3, 8, 7, 9, 4, 6, 5, 10, 11, 12, 13} *)

Here, it's 7, 9, 4 instead of 4, 7, 9. This is because we know that

If some of the f[e[i]] are the same, then the canonical order of the corresponding e[i] is used.

from the documentation of SortBy. This could go either way, I guess: I can see how it would be reasonable to keep the ordering intact if the elements are "equal" in the ordering sense, but I also like the idea that SortBy respects Mathematica's implicit sorting. I suppose both ways can be useful in different situations.

Mathematica's sorting algorithm for expressions is something of a mystery to me, but note that

Ordering[mylist]
(* {8, 7, 9, 6, 5, 10, 4, 3, 2, 11, 1, 12, 13} *)

We can see that the order is 7, 9, 4, although of course some other elements of the list come between some of them.


Finally, as noted by Mr. Wizard, one can reproduce the Ordering solution with SortBy using

SortBy[mylist, {sortFunc}]
$\endgroup$
1
  • 1
    $\begingroup$ SortBy[mylist, {sortFunc}] will provide a stable sort matching the result of Ordering. References: (3332690), (7687), (27323), (31926) $\endgroup$
    – Mr.Wizard
    Sep 9, 2015 at 21:45
7
$\begingroup$

Using your code, let me make it clear by introducing an auxiliary function that sums the exponents in an expression

sortFunc[expr_] := Total[Exponent[expr, x, List]]

Now you can go with

Sort[mylist, sortFunc[#1] < sortFunc[#2] &]
$\endgroup$

Your Answer

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

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