<searchSorted>
<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)
asListboolean. Default value: true. Whether to render the items separated by commas (true) or with no separator (false).
sidekeyword. Which end of a run of equal values the target is placed at.
| Value | Description |
|---|---|
left (default) | Insert before any equal values, so the result is the index of the first entry greater than or equal to the target. |
right | Insert 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.
typekeyword. 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.
| Value | Description |
|---|---|
number | Read bare strings as numbers, ordered by value. |
math | Read bare strings as math expressions, ordered by value. |
text | Read bare strings as text, ordered alphabetically. |
boolean | Read bare strings as booleans, ordered with false before true. |
Other (5)
avoidScientificNotationWhether to render numbers in full decimal form rather than scientific notation.
displayDecimalsNumber of decimal places to display when rendering this number.
displayDigitsNumber of significant digits to display when rendering this number.
displaySmallAsZeroThreshold below which numbers are displayed as zero.
padZerosWhether to pad displayed numbers with trailing zeros to fill the requested digits/decimals.
Common to all components (9)
copyreference. Create an independent copy of another component by reference. Enter a references a $name.
disabledboolean. Default value: false. Whether this component is disabled and cannot be interacted with.
extendreference. Extend another component by reference, inheriting its children and attributes. Enter a reference as $name.
fixedDefault value: false. Whether this component's value is fixed and cannot be modified.
fixLocationboolean. Default value: false. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).
hideboolean. Default value: false. Whether to hide this component from the rendered output.
isResponseboolean. Default value: false. Whether this component is treated as a response for the purposes of assessment.
nametext. The name used to reference this component from elsewhere in the document.
styleNumberinteger. 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.sidetext. Which end of a run of equal values the target is placed at.
Common to all components (4)
$s.doenetMLtext. The DoenetML source code that produced this component.
$s.hideboolean. Whether to hide this component from the rendered output.
$s.isResponseboolean. Whether this component is treated as a response for the purposes of assessment.
$s.styleNumberinteger. 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.