Skip to main content

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Downloads1/wk
Published2 years ago (0.215.0)
function maxWith
maxWith<T>(
array: Iterable<T>,
comparator: (
a: T,
b: T
) => number
): T | undefined

Returns the first element having the largest value according to the provided comparator or undefined if there are no elements.

The comparator is expected to work exactly like one passed to Array.sort, which means that comparator(a, b) should return a negative number if a < b, a positive number if a > b and 0 if a === b.

Examples

Example 1

import { maxWith } from "@std/collections/max_with";
import { assertEquals } from "@std/assert/assert_equals";

const people = ["Kim", "Anna", "John", "Arthur"];
const largestName = maxWith(people, (a, b) => a.length - b.length);

assertEquals(largestName, "Arthur");

Type Parameters

Parameters

array: Iterable<T>
comparator: (
a: T,
b: T
) => number

Return Type

T | undefined

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/collections

Import symbol

import { maxWith } from "@std/collections/max_with";
or

Import directly with a jsr specifier

import { maxWith } from "jsr:@std/collections/max_with";