as_ordered_sequence: Build an Ordered Sequence from 'x' and 'keys'

View source: R/50-ordered_sequence-constructors.R

as_ordered_sequenceR Documentation

Build an Ordered Sequence from x and keys

Description

Constructs an ordered_sequence by pairing each element of x with the corresponding key in keys.

Usage

as_ordered_sequence(x, keys)

Arguments

x

Elements to add.

keys

Key values with the same length as x.

Details

Output is always sorted by key.

Duplicate keys are allowed; ties preserve input order (stable/FIFO within the same key).

Names on x are preserved as element names.

Value

An ordered_sequence.

Examples

xs <- as_ordered_sequence(c("d", "a", "b", "a2"), keys = c(4, 1, 2, 1))
xs
length(elements_between(xs, 1, 1))

n <- as_ordered_sequence(setNames(as.list(c("a", "b")), c("ka", "kb")), keys = c(2, 1))
n[["kb"]]

# Keys can be other comparable types
num_by_chr <- as_ordered_sequence(c(20, 10, 30), keys = c("b", "a", "c"))
num_by_chr

Immutables documentation built on April 29, 2026, 1:06 a.m.