MaximalBy
MaximalBy[data,f]
returns a list of the elements ei of data for which the value of f[ei] is maximal.
MaximalBy[data,f,n]
returns a list of the elements ei of data corresponding to the n largest f[ei].
MaximalBy[data,f,n,p]
uses the ordering function p for sorting.
Details
- By default, values of f[ei] are compared using Order, the same canonical order as in Sort.
- MaximalBy[data,f] returns the list of maximal elements ei of data in the order they appear in the input.
- MaximalBy[data,f,n] returns the ei sorted in the order of decreasing f[ei], with those having the same value of f[ei] being taken in the order they appear in 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 » - For tabular data tab, MaximalBy[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.
- MaximalBy[data,f, UpTo[n]] gives n elements, or as many as are available. »
- MaximalBy[f][data] is equivalent to MaximalBy[data,f]. »
Examples
open allclose allBasic Examples (4)
Find the maximal element by its last part:
Do the same using the operator form of MaximalBy:
All maximal elements are returned, in order of appearance:
Scope (10)
Obtain the first four maximal elements or as many as are available:
MaximalBy works with symbolic expressions, using canonical Order by default:
Find maximal element in a list of comparable quantities with various units:
Comparing by QuantityMagnitude loses the unit information:
Find numerically largest element:
MaximalBy works on QuantityArray:
MaximalBy will order dates according to canonical order by default:
Convert the dates to absolute times to sort them numerically:
Equivalently, convert the dates to DateObject form and use NumericalOrder instead of Order:
Take the letters of the Polish alphabet:
Transliterate them to the Hiragana script:
These are the five largest Polish letters according to canonical order:
These are the five largest Polish letters according to the rules of the Polish alphabet:
These are the five largest Polish letters according to canonical order of their Hiragana transliteration:
These are the five largest Polish letters according to alphabetic order in Japanese of their transliteration:
Construct a TabularColumn object with 100 words:
Select the five longest words:
Normalize the result to a list:
Find the four rows in a Tabular object with largest values in a given column:
Use general functional notation instead of the column name:
Take a dataset of the solar system planets:
Find the three planets with the maximal number of moons:
When there are common values of f[ei] for different elements ei, the original order will be kept:
Applications (3)
Find the four longest texts available in ExampleData["Text"]:
Find the five constellations with maximal number of bright stars:
Properties & Relations (3)
MaximalBy[{e1,e2,…},f,n] compares values f[ei] using canonical Order:
TakeLargestBy[{e1,e2,…},f,n] compares values f[ei] using NumericalOrder:
For a specific ordering function p, MaximalBy[data,f,n,p] is equivalent to TakeLargestBy[data,f,n,p]:
Possible Issues (1)
By default, the maximal element is determined using canonical Order, not numerical ordering:
Text
Wolfram Research (2014), MaximalBy, Wolfram Language function, https://reference.wolfram.com/language/ref/MaximalBy.html (updated 2025).
CMS
Wolfram Language. 2014. "MaximalBy." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/MaximalBy.html.
APA
Wolfram Language. (2014). MaximalBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MaximalBy.html