ReferencesearchSorted

<searchSorted>

The position at which each target would be inserted to keep a sorted list sorted

<searchSorted> is a General Operator component that locates values within an already sorted list: it renders the position at which each target would have to be inserted for the list to stay sorted. Unlike <indexOf>, the target need not appear in the list — this answers “where does it belong?”, not “is it there?”.

Attributes and Properties

Attributes for <searchSorted>

Highlighted (4)
asList

boolean. Default value: true. Whether to render the items separated by commas (true) or with no separator (false).

side

keyword. Which end of a run of equal values the target is placed at.

ValueDescription
left (default)Insert before any equal values, so the result is the index of the first entry greater than or equal to the target.
rightInsert after any equal values, so the result is the index of the first entry strictly greater than the target.

_componentListWithSelectableType. The value, or list of values, to locate within the sorted list.

type

keyword. Component type to interpret bare string children as. Omit it and they are read as what they look like: every piece naming a number makes the list numeric, anything else makes it text. Also decides how target is read, since it has no type of its own.

ValueDescription
numberRead bare strings as numbers, ordered by value.
mathRead bare strings as math expressions, ordered by value.
textRead bare strings as text, ordered alphabetically.
booleanRead bare strings as booleans, ordered with false before true.
Other (5)
avoidScientificNotation

Whether to render numbers in full decimal form rather than scientific notation.

displayDecimals

Number of decimal places to display when rendering this number.

displayDigits

Number of significant digits to display when rendering this number.

displaySmallAsZero

Threshold below which numbers are displayed as zero.

padZeros

Whether to pad displayed numbers with trailing zeros to fill the requested digits/decimals.

Common to all components (9)
copy

reference. Create an independent copy of another component by reference. Enter a references a $name.

disabled

boolean. Default value: false. Whether this component is disabled and cannot be interacted with.

extend

reference. Extend another component by reference, inheriting its children and attributes. Enter a reference as $name.

fixed

Default value: false. Whether this component's value is fixed and cannot be modified.

fixLocation

boolean. Default value: false. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).

hide

boolean. Default value: false. Whether to hide this component from the rendered output.

isResponse

boolean. Default value: false. Whether this component is treated as a response for the purposes of assessment.

name

text. The name used to reference this component from elsewhere in the document.

styleNumber

integer. Default value: 1. The style number used to select this component's visual styling from the available style definitions.

Properties for <searchSorted name="s">

Highlighted (1)
$s.side

text. Which end of a run of equal values the target is placed at.

Common to all components (4)
$s.doenetML

text. The DoenetML source code that produced this component.

$s.hide

boolean. Whether to hide this component from the rendered output.

$s.isResponse

boolean. Whether this component is treated as a response for the purposes of assessment.

$s.styleNumber

integer. The style number used to select this component's visual styling from the available style definitions.

Examples

Example: where a value belongs

A target smaller than everything gives 1; a target larger than everything gives 5, one past the last index.


Example: sampling from a weighted population

This is the standard way to sample from a discrete distribution: accumulate the weights with <cumulativeSum>, draw uniformly from the total, then use <searchSorted> to find which bucket the draw landed in. Because every individual in the population is equally likely to be drawn, each subpopulation is selected in proportion to its size.


Example: many draws at once

Raising numSamples here does not add searches: target takes the whole list of draws and one <searchSorted> reports a subpopulation for each. Every result is still a component of its own, so the results grow with the sample — but wrapping the draws in a <repeat> to search them one at a time would build a whole operator for every draw on top of that, which stops being practical long before the sample is large enough to be interesting.


Example: side and repeated values

When the target equals several entries, side chooses which end of that run to report: left gives the first of them, right gives the position just after the last.


Example: a sorted list of words

Only an ordering is needed, not arithmetic, so a sorted text list works the same way.


Attribute Examples

Attribute Example: target

target is read the way bare children are: whitespace separates one target from the next, so target="New York" asks where two separate words belong and gets two answers. A value with a space in it is written as a <text> of its own and referenced — which is also how the sorted list is built, since <textList>Boston Chicago New York</textList> is four entries and not three.