My problem is that I have a defined function, say, foo, and I am using the built in Sort function to order a sequence of calls

foo[x1], food[x2], ..., foo[xn] 

from lowest value to greatest.


Sort[{foo[x1], foo[x2]}, ..., foo[xn]] 

outputs the numerical evaluation of foo[xi]

so that (for example) I get an output like

 {1.1, 3.9 ,..., 9.7}

Assuming foo[x11] evaluates to the lowest, foo[x3] the second lowest and foo[x108] the greatest numerical value, I want the output to be

{foo[x11], foo[x3], ..., foo[x108]}

I also tried

 HoldForm[Sort[{foo[x1], foo[x2]},...,foo[xn]]] 

and using HoldForm on the individual elements given to Sort, but neither worked.

Perhaps there is a simpler way of doing what I want to do (list me the order of foo[xi] from lowest value to greatest). Any ideas?

  • 1
    $\begingroup$ Maybe you can use SortBy: SortBy[{1.1, 3.9, ..}, foo] $\endgroup$
    – Carl Woll
    Feb 3 '18 at 20:33
  • $\begingroup$ A concrete example is needed here. Otherwise people have to guess what is wanted. $\endgroup$ Feb 4 '18 at 1:11
foo[x_] = x;


list = (Inactive[foo] /@ RandomInteger[{100}, 5])

enter image description here

SortBy[list, Activate]

enter image description here

foo = #^2&;
list = RandomReal[{-1, 1}, 5]
Inactive[foo] /@ SortBy[list, foo]

enter image description here

Defer[foo] /@ SortBy[list, foo]



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.