2
$\begingroup$

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.

However,

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?

$\endgroup$
  • 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$ – Daniel Lichtblau Feb 4 '18 at 1:11
2
$\begingroup$
foo[x_] = x;

SeedRandom[0]

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

enter image description here

SortBy[list, Activate]

enter image description here

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

enter image description here

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

{foo[0.02234],foo[0.027387],foo[0.070565],foo[0.20430],foo[-0.91121]}

$\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.