maxWith
import { maxWith } from "jsr:@ende/collections";function
function maxWith<T>(array: Iterable<T>, comparator: (a: T, b: T) => number): T | undefinedReturns 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.
array: Iterable<T>The array to find the maximum element in.
comparator: (a: T, b: T) => numberThe function to compare elements.
Returns
The first element that is the largest value of the given function or undefined if there are no elements.
Example: Basic usage
import { maxWith } from "@ende/collections/max-with";
import { assertEquals } from "@ende/assert";
const people = ["Kim", "Anna", "John", "Arthur"];
const largestName = maxWith(people, (a, b) => a.length - b.length);
assertEquals(largestName, "Arthur");