TakeLargestBy
✖
TakeLargestBy
gives the n elements ei in data for which f[ei] is largest, sorted in descending order.
gives the property prop for the n elements in data for which f[ei] is largest.
represents an operator form of TakeLargestBy that can be applied to an expression.
Details and Options


- TakeLargestBy gives the elements ei in the first level of the input data corresponding to the largest f[ei] values.
- By default, TakeLargestBy uses NumericalOrder to sort f[ei] values that are numeric expressions, Quantity objects and DateObject expressions. For other types of objects, TakeLargestBy sorts the f[ei] values by canonical Order.
- In TakeLargestBy[data,…], the data can have the following forms:
-
{e1,e2,…} list of values, including numbers, quantities, dates, ... Association[…] association of values » QuantityArray[…] quantity array or other structured array Tabular[…] type-consistent tabular data » TabularColumn[…] type-consistent column data » Dataset[…] general hierarchical data » - TakeLargestBy[assoc,f,n] gives an association of length n by taking the values in assoc that are largest according to f, preserving their keys.
- For tabular data tab, TakeLargestBy[tab,f,…] applies the function f to individual rows of tab, with the row being an association <col1val1,… > if tab has column keys or a list {val1,…} if tab does not have column keys.
- In TakeLargestBy[dataprop,f,n], possible forms for prop include:
-
"Element" gives each element itself » "Index" gives the index for each element » "Value" gives the value f[x] for each element x » {prop1,prop2,…} a list of multiple forms » All gives an association with element, index and f value » - TakeLargestBy[data,f,UpTo[n]] takes n elements, or as many as are available. »
- TakeLargestBy[f,n][data] is equivalent to TakeLargestBy[data,f,n].
- TakeLargestBy has option ExcludedForms. With the default setting ExcludedForms->Automatic, TakeLargestBy excludes from its final results elements for which f[ei] is None, Null or Indeterminate or has head Missing. »
- The setting ExcludedForms->{patt1,patt2,…} specifies that expressions for which f[ei] matches any of the patti should be excluded from results generated by TakeLargestBy. »
Examples
open allclose allBasic Examples (5)Summary of the most common use cases
Give the four numbers farthest from zero:

https://wolfram.com/xid/0jz9yhkzlcgzwt-quxmyv

Take the two longest strings in a list:

https://wolfram.com/xid/0jz9yhkzlcgzwt-xh63mx

Do the same using the operator form of TakeLargestBy:

https://wolfram.com/xid/0jz9yhkzlcgzwt-cvc29l


https://wolfram.com/xid/0jz9yhkzlcgzwt-l7nlaj

Take the three largest Quantity lengths according to their distance to :

https://wolfram.com/xid/0jz9yhkzlcgzwt-6t1czq

Take the three DateObject expressions with the largest day of the month:

https://wolfram.com/xid/0jz9yhkzlcgzwt-4s19fq


https://wolfram.com/xid/0jz9yhkzlcgzwt-vm67dt

Take the two longest strings in an association:

https://wolfram.com/xid/0jz9yhkzlcgzwt-jgfph4

Scope (8)Survey of the scope of standard use cases
Give the four numbers farthest from zero, or as many as are available if fewer:

https://wolfram.com/xid/0jz9yhkzlcgzwt-ws9yis

The discovery date value for some planets is missing:

https://wolfram.com/xid/0jz9yhkzlcgzwt-il3p2i


https://wolfram.com/xid/0jz9yhkzlcgzwt-oh3twu

By default, Missing[] and several other symbolic expressions are excluded if returned by the selection function:

https://wolfram.com/xid/0jz9yhkzlcgzwt-j5oflm

Get the two largest elements by magnitude in a list:

https://wolfram.com/xid/0jz9yhkzlcgzwt-henpdc

https://wolfram.com/xid/0jz9yhkzlcgzwt-5yfcey

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

https://wolfram.com/xid/0jz9yhkzlcgzwt-cr2dko

Get the two largest magnitudes in a list:

https://wolfram.com/xid/0jz9yhkzlcgzwt-if590k

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

https://wolfram.com/xid/0jz9yhkzlcgzwt-i70n6a

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

https://wolfram.com/xid/0jz9yhkzlcgzwt-dw04qo

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

https://wolfram.com/xid/0jz9yhkzlcgzwt-21mfoi


https://wolfram.com/xid/0jz9yhkzlcgzwt-gd1pkd
They can be translated to Spanish with WordTranslation, which gives a list of possibilities:

https://wolfram.com/xid/0jz9yhkzlcgzwt-ufmytu

https://wolfram.com/xid/0jz9yhkzlcgzwt-z63mfq

These are the largest two words according to canonical order:

https://wolfram.com/xid/0jz9yhkzlcgzwt-6zdtb7

These are the largest two English words according to canonical order of their Spanish translations:

https://wolfram.com/xid/0jz9yhkzlcgzwt-4utuab

Use properties to show simultaneously the English word and its translation to Spanish:

https://wolfram.com/xid/0jz9yhkzlcgzwt-gkizxz

Construct a TabularColumn object with 100 words:

https://wolfram.com/xid/0jz9yhkzlcgzwt-ss77bg

Select the five longest words:

https://wolfram.com/xid/0jz9yhkzlcgzwt-ije4w6

Normalize the result to a list:

https://wolfram.com/xid/0jz9yhkzlcgzwt-5kepgn

Find the four rows in a Tabular object with largest values in a given column:

https://wolfram.com/xid/0jz9yhkzlcgzwt-f1trbx


https://wolfram.com/xid/0jz9yhkzlcgzwt-y5poc

Use general functional notation instead of the column name:

https://wolfram.com/xid/0jz9yhkzlcgzwt-2shaqt


https://wolfram.com/xid/0jz9yhkzlcgzwt-r1yvkm

Take a dataset of the solar system planets:

https://wolfram.com/xid/0jz9yhkzlcgzwt-2sxsoh

Find the three planets with the largest number of moons:

https://wolfram.com/xid/0jz9yhkzlcgzwt-3981jc

Take a list of integer 3-vectors:

https://wolfram.com/xid/0jz9yhkzlcgzwt-pyk5yx


https://wolfram.com/xid/0jz9yhkzlcgzwt-uzhb9e

Select the three vectors with largest norms, by default sorting numerically those norms:

https://wolfram.com/xid/0jz9yhkzlcgzwt-9chmoz

Sorting the norms by canonical order can give a different result:

https://wolfram.com/xid/0jz9yhkzlcgzwt-266zb3

Options (2)Common values & functionality for each option
ExcludedForms (2)
Take the list of all planets in the solar system:

https://wolfram.com/xid/0jz9yhkzlcgzwt-o9kj4i

Find the four planets that are currently farthest from your location, with a limit of 15 au in distance:

https://wolfram.com/xid/0jz9yhkzlcgzwt-o1c69u

Plot the evolution of the distances during a two-year period centered at the current moment:

https://wolfram.com/xid/0jz9yhkzlcgzwt-ejty5f

https://wolfram.com/xid/0jz9yhkzlcgzwt-bb1ah6


https://wolfram.com/xid/0jz9yhkzlcgzwt-gg93zh

Not all these keys have values in the association:

https://wolfram.com/xid/0jz9yhkzlcgzwt-wkaz6w

https://wolfram.com/xid/0jz9yhkzlcgzwt-wna32b

Find the keys corresponding to the two largest values in the association:

https://wolfram.com/xid/0jz9yhkzlcgzwt-bonkqh

By default, some symbolic objects like Missing[] or None are excluded as results of the selection function, so the previous result is equivalent to this:

https://wolfram.com/xid/0jz9yhkzlcgzwt-9kqzz5

Specify that no value should be excluded:

https://wolfram.com/xid/0jz9yhkzlcgzwt-rjnsp0

Specify that only values with Missing head should be excluded:

https://wolfram.com/xid/0jz9yhkzlcgzwt-fy7rmm

Applications (6)Sample problems that can be solved with this function
Find the eight capitals of continental US states that are farthest from Kansas City:

https://wolfram.com/xid/0jz9yhkzlcgzwt-et8buy


https://wolfram.com/xid/0jz9yhkzlcgzwt-66q9wg

Find the five most recent James Bond movies:

https://wolfram.com/xid/0jz9yhkzlcgzwt-l5oypm

Find the 10 countries with the longest names:

https://wolfram.com/xid/0jz9yhkzlcgzwt-fcthyl

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

https://wolfram.com/xid/0jz9yhkzlcgzwt-wfok81

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

https://wolfram.com/xid/0jz9yhkzlcgzwt-f0nqbi

Find the five constellations with largest number of bright stars, showing how many each one has:

https://wolfram.com/xid/0jz9yhkzlcgzwt-y9s8ve

Properties & Relations (3)Properties of the function, and connections to other functions
TakeLargestBy[list,f,n,p] is effectively equivalent to Part[list,TakeLargest[Map[f,list]"Index",n,p]]:

https://wolfram.com/xid/0jz9yhkzlcgzwt-iezsol

https://wolfram.com/xid/0jz9yhkzlcgzwt-dsovx7


https://wolfram.com/xid/0jz9yhkzlcgzwt-s7dcf5

TakeLargestBy[{e1,e2,…},f,n] compares values f[ei] using NumericalOrder by default:

https://wolfram.com/xid/0jz9yhkzlcgzwt-gih291

https://wolfram.com/xid/0jz9yhkzlcgzwt-dfljwn


https://wolfram.com/xid/0jz9yhkzlcgzwt-82pmav

MaximalBy[{e1,e2,…},f,n] compares values f[ei] using canonical Order by default:

https://wolfram.com/xid/0jz9yhkzlcgzwt-d1gqp2


https://wolfram.com/xid/0jz9yhkzlcgzwt-trdyiu

Both TakeLargestBy and MaximalBy take an ordering function as fourth argument, which makes them effectively equivalent:

https://wolfram.com/xid/0jz9yhkzlcgzwt-0ihvu1


https://wolfram.com/xid/0jz9yhkzlcgzwt-mgg1cj

When there are common values of f[ei] for different elements ei in TakeLargestBy[{e1,e2,…},f,n], the original order will be kept:

https://wolfram.com/xid/0jz9yhkzlcgzwt-eie171


https://wolfram.com/xid/0jz9yhkzlcgzwt-7xt8dc


https://wolfram.com/xid/0jz9yhkzlcgzwt-3qjjjv

Possible Issues (2)Common pitfalls and unexpected behavior
If fewer than the requested number of elements are present, TakeLargestBy will not evaluate:

https://wolfram.com/xid/0jz9yhkzlcgzwt-wv21bw


Use UpTo to get as many elements as possible:

https://wolfram.com/xid/0jz9yhkzlcgzwt-pu5pzo

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

https://wolfram.com/xid/0jz9yhkzlcgzwt-wkg1mn


Wolfram Research (2015), TakeLargestBy, Wolfram Language function, https://reference.wolfram.com/language/ref/TakeLargestBy.html (updated 2025).
Text
Wolfram Research (2015), TakeLargestBy, Wolfram Language function, https://reference.wolfram.com/language/ref/TakeLargestBy.html (updated 2025).
Wolfram Research (2015), TakeLargestBy, Wolfram Language function, https://reference.wolfram.com/language/ref/TakeLargestBy.html (updated 2025).
CMS
Wolfram Language. 2015. "TakeLargestBy." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/TakeLargestBy.html.
Wolfram Language. 2015. "TakeLargestBy." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/TakeLargestBy.html.
APA
Wolfram Language. (2015). TakeLargestBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TakeLargestBy.html
Wolfram Language. (2015). TakeLargestBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TakeLargestBy.html
BibTeX
@misc{reference.wolfram_2025_takelargestby, author="Wolfram Research", title="{TakeLargestBy}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/TakeLargestBy.html}", note=[Accessed: 07-June-2025
]}
BibLaTeX
@online{reference.wolfram_2025_takelargestby, organization={Wolfram Research}, title={TakeLargestBy}, year={2025}, url={https://reference.wolfram.com/language/ref/TakeLargestBy.html}, note=[Accessed: 07-June-2025
]}