<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?β.
The list has to be in ascending order, and <searchSorted> checks that it is. Given
values that are not in order it reports 0 instead of a position β the same βno positionβ it
gives when target is missing β and warns, rather than returning a number that looks like
a position but is not one. Equal neighbors are in order; that run is what side is about.
Write allowUnsorted on the operator and it reports where each target belongs among the values in
ascending order, whatever order they are written in β so a list that is not sorted can be
searched directly. The values themselves are left as they are.
Attributes and Properties
Attributes for <searchSorted>
Highlighted (5)
allowUnsortedIn developmentboolean. Default value: false. Report where each target belongs among the values in ascending order, whatever order they are written in.
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. |
Number display (5)
avoidScientificNotationboolean. Default value: false. Whether to render numbers in full decimal form rather than scientific notation.
displayDecimalsinteger. Default value: 2. Number of decimal places to display when rendering this number.
displayDigitsinteger. Default value: 3. Number of significant digits to display when rendering this number.
displaySmallAsZeronumber. Default value: 1e-14. Threshold below which numbers are displayed as zero.
padZerosboolean. Default value: false. Whether 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.
fixedboolean. Default 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 (2)
$s.allowUnsortedIn developmentboolean. Report where each target belongs among the values in ascending order, whatever order they are written in.
$s.sidetext. Which end of a run of equal values the target is placed at.
Number display (5)
$s.avoidScientificNotationIn developmentboolean. Whether to render numbers in full decimal form rather than scientific notation.
$s.displayDecimalsIn developmentinteger. Number of decimal places to display when rendering this number.
$s.displayDigitsIn developmentinteger. Number of significant digits to display when rendering this number.
$s.displaySmallAsZeroIn developmentnumber. Threshold below which numbers are displayed as zero.
$s.padZerosIn developmentboolean. Whether to pad displayed numbers with trailing zeros to fill the requested digits/decimals.
Common to all components (8)
$s.disabledIn developmentboolean. Whether this component is disabled and cannot be interacted with.
$s.doenetMLtext. The DoenetML source code that produced this component.
$s.fixedIn developmentboolean. Whether this component's value is fixed and cannot be modified.
$s.fixLocationIn developmentboolean. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).
$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: the list has to be sorted
The first <searchSorted> gets 0 and a warning: 87 comes before 75, so no single
position describes where 80 belongs. Sorting the values first β with
<sort> here, though a list that is already built in order, such as a
<cumulativeSum> of positive numbers, needs nothing β gives a
position again. The last line gets there directly: allowUnsorted hands the ordering to the
operator, which reports where 80 belongs among these values in ascending order without
rearranging them. side goes on meaning what it means.
Prefer allowUnsorted when the ordering is wanted only for the search. Keep a separate
<sort> when the sorted values are themselves worth showing or reusing, as
$sorted is above.
This matters most when the values change as the activity runs. A list assembled from
inputs can be out of order while it is being filled in, and <searchSorted> answers
0 for exactly as long as it is.
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.