TakeLargestBy

TakeLargestBy[list,f,n]

gives the n elements ei in list for which f[ei] is largest, sorted in descending order.

TakeLargestBy[listprop,f,n]

gives the property prop for the n elements in list for which f[ei] is largest.

TakeLargestBy[f,n]

represents an operator form of TakeLargestBy that can be applied to an expression.

Details and Options

Examples

open allclose all

Basic Examples  (3)

Take the two longest strings in a list:

Do the same using the operator form of TakeLargestBy:

Take the two longest strings in an association:

Give the four numbers farthest from zero:

Give the 4 numbers farthest from zero, or as many as are available if fewer:

Scope  (5)

Get the two largest elements by magnitude in a list:

Get the positions of the two largest elements by magnitude in a list:

Get the two largest elements by magnitude in a list along with their positions:

Get the two largest elements by magnitude in a list with their positions given first:

Get associations containing the element and position of the two largest elements by magnitude in a list:

Find the three capitals of continental US states that are farthest from Kansas City:

Find the five most recent James Bond movies:

Find the 15 countries with the longest names:

Applications  (1)

Find the four US states with the largest number of neighboring states, and show them:

Possible Issues  (2)

If fewer than the requested number of elements are present, TakeLargestBy will not evaluate:

If the f[ei] are not comparable, TakeLargestBy will not evaluate:

Neat Examples  (1)

From the BRICS group of countries, give the country that is farthest from the current location:

Introduced in 2015
 (10.1)
 |
Updated in 2019
 (12.0)