Skip to content

proposal: cmp: add CompareBy functionΒ #71238

@coady

Description

@coady

Proposal Details

An addition to the cmp package to support a common use case in ordered comparison.

func CompareBy[K cmp.Ordered, V any](key func(V) K) func(V, V) int {
	return func(a, b V) int { return cmp.Compare(key(a), key(b)) }
}

The majority of custom comparison functions apply the same transformation to each element. Analogous to Python's key functions, and would integrate well with Reverse proposed in #65632.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions