Function Repository Resource:

PositionLargestBy

Source Notebook

The position of the elements in a list that are largest according to a given function

Contributed by: Bob Sandheinrich

ResourceFunction["PositionLargestBy"][list,f]

gives the position of the first element in list that has the largest value when f is applied to it.

ResourceFunction["PositionLargestBy"][list,f,n]

gives the positions of n values that are largest.

Details and Options

f must give numeric values for the elements of list.
UpTo[n] is supported.

Examples

Basic Examples (2) 

Find a position of the integer with the largest inverse:

In[1]:=
ResourceFunction["PositionLargestBy"][{100, 200, 100, 200, 50}, 1/# &]
Out[1]=

Get a list of the four positions of largest inverses:

In[2]:=
ResourceFunction["PositionLargestBy"][{100, 200, 100, 200, 50}, 1/# &,
  4]
Out[2]=

Scope (2) 

Retrieve data on moons in our solar system:

In[3]:=
moons = Values /@ ResourceData["Sample Data: Solar System Planets and Moons"][All, "Moons", Select[FreeQ[_Missing | _Interval]]]
Out[3]=

Find the positions of the two densest moons for each planet using UpTo:

In[4]:=
ResourceFunction[
   "PositionLargestBy"][#, (QuantityMagnitude[#Mass/#Radius^3] &), UpTo[2]] & /@ moons
Out[4]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 11 October 2018

License Information