MaximalBy
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 all close allBasic Examples (4)
Find the maximal element by its last part:
MaximalBy[{{a, 1}, {b, 1}, {a, 2}, {d, 1}, {b, 3}}, Last]Do the same using the operator form of MaximalBy:
MaximalBy[Last][{{a, 1}, {b, 1}, {a, 2}, {d, 1}, {b, 3}}]All maximal elements are returned, in order of appearance:
MaximalBy[{{a, 1}, {b, 1}, {b, 3}, {d, 2}, {a, 3}}, Last]Obtain the first three maximal elements:
MaximalBy[{{a, 1}, {b, 1}, {b, 3}, {d, 2}, {a, 3}}, Last, 3]Prune an association to its maximal values:
MaximalBy[<|"a" -> {1, 1}, "b" -> {2, 3}, "c" -> {4, 2}, "d" -> {1, 3}|>, Last]Scope (10)
Obtain the first four maximal elements or as many as are available:
MaximalBy[{{a, 1}, {b, 1}, {b, 3}}, First, UpTo[4]]MaximalBy works with symbolic expressions, using canonical Order by default:
MaximalBy[{{a, 1}, {b, 1}, {b, 3}, {d, 2}, {a, 3}}, First]Find maximal element in a list of comparable quantities with various units:
data = {Quantity[3, "Feet"], Quantity[9, "Inches"], Quantity[1, "Meters"]};Comparing by QuantityMagnitude loses the unit information:
MaximalBy[data, QuantityMagnitude]Find numerically largest element:
MaximalBy[data, NumericalSort]MaximalBy works on QuantityArray:
data = QuantityArray[RandomReal[1, {6, 2}], {"Meters", "Seconds"}]MaximalBy[data, Last]//NormalMaximalBy will order dates according to canonical order by default:
dates = {DateObject[{2024, 9, 12}], "12 Sept 2022", {2023, 9, 12}};MaximalBy[dates, Identity]Convert the dates to absolute times to sort them numerically:
MaximalBy[dates, AbsoluteTime]Equivalently, convert the dates to DateObject form and use NumericalOrder instead of Order:
MaximalBy[dates, DateObject, 1, NumericalOrder]Take the letters of the Polish alphabet:
letters = Entity["Alphabet", "Polish::7949q"]["CommonAlphabet"]Transliterate them to the Hiragana script:
hiragana = Transliterate[letters, "Polish" -> "Hiragana"]These are the five maximal Polish letters according to canonical order:
MaximalBy[letters, Identity, 5]These are the five maximal Polish letters according to the rules of the Polish alphabet:
MaximalBy[letters, Identity, 5, AlphabeticOrder["Polish"]]These are the five maximal Polish letters according to canonical order of their Hiragana transliteration:
MaximalBy[letters, Transliterate[#, "Polish" -> "Hiragana"]&, 5]These are the five maximal Polish letters according to alphabetic order in Japanese of their transliteration:
MaximalBy[letters, Transliterate[#, "Polish" -> "Hiragana"]&, 5, AlphabeticOrder["Japanese"]]Construct a TabularColumn object with 100 words:
col = TabularColumn[RandomWord[100]]Select the five longest words:
MaximalBy[col, StringLength, 5]Normalize the result to a list:
Normal[%]Find the four rows in a Tabular object with largest values in a given column:
data = {{8, 13, 18, 8, 14, 13, 18, 27}, {16.1, 29.2, 23.8, 14.7, 30.1, 27.5, 35.1, 36.2}};
tab = ToTabular[data, "Columns", {"size", "length"}]MaximalBy[tab, "length", 4]Use general functional notation instead of the column name:
MaximalBy[tab, #length&, 4]MaximalBy[tab, (#length - #size)&, 4]Take a dataset of the solar system planets:
dataset = Dataset[ExampleData[{"Dataset", "Planets"}], MaxItems -> 4]Find the three planets with the maximal number of moons:
MaximalBy[dataset, Length[#Moons]&, 3]//KeysWhen there are common values of f[ei] for different elements ei, the original order will be kept:
list = Range[-5, 5]MaximalBy[list, Abs, 4]MaximalBy[Reverse[list], Abs, 4]Applications (3)
Find the four longest texts available in ExampleData["Text"]:
MaximalBy[ExampleData["Text"], StringLength @* ExampleData, 4]Find the five constellations with maximal number of bright stars:
MaximalBy[ConstellationData[], Length @* EntityProperty["Constellation", "BrightStars"], 5]Take a dataset of the solar system planets:
dataset = Dataset[ExampleData[{"Dataset", "Planets"}], MaxItems -> 4]Find the two planets with the maximal mass:
MaximalBy[dataset, #Mass&, 2]//KeysProperties & Relations (3)
MaximalBy[{e1,e2,…},f,n] compares values f[ei] using canonical Order:
data = {{1, x}, {0, x}, {-Infinity, x}};MaximalBy[data, First, 2]ReverseSort[First /@ data, Order]TakeLargestBy[{e1,e2,…},f,n] compares values f[ei] using NumericalOrder:
TakeLargestBy[data, First, 2]ReverseSort[First /@ data, NumericalOrder]For a specific ordering function p, MaximalBy[data,f,n,p] is equivalent to TakeLargestBy[data,f,n,p]:
planets = PlanetData[]MaximalBy[planets, EntityProperty["Planet", "Name"], 4, AlphabeticOrder]TakeLargestBy[planets, EntityProperty["Planet", "Name"], 4, AlphabeticOrder]For association, the function f is applied to values:
assoc = <|"a" -> {1, 1}, "b" -> {2, 1}, "c" -> {2, 3}, "d" -> {4, 2}, "e" -> {1, 3}|>;Values[MaximalBy[assoc, First]] === MaximalBy[Values[assoc], First]Possible Issues (1)
By default, the maximal element is determined using canonical Order, not numerical ordering:
MaximalBy[{1, 2, 3, Pi, 4}, Identity]MaximalBy[{BesselJ[0, 1], BesselJ[1, 1]}, Identity]Compare numerical values of the elements of the list:
MaximalBy[{1, 2, 3, Pi, 4}, N]MaximalBy[{BesselJ[0, 1], BesselJ[1, 1]}, N]See Also
MinimalBy Max TakeLargest TakeLargestBy FindMaximum Maximize RankedMax ReverseSortBy Ordering
Function Repository: Maximal
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
BibTeX
@misc{reference.wolfram_2026_maximalby, author="Wolfram Research", title="{MaximalBy}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/MaximalBy.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_maximalby, organization={Wolfram Research}, title={MaximalBy}, year={2025}, url={https://reference.wolfram.com/language/ref/MaximalBy.html}, note=[Accessed: 13-June-2026]}