Skip to Content
ReferencesearchSorted

<searchSorted>

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

<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 development

boolean. Default value: false. Report where each target belongs among the values in ascending order, whatever order they are written in.

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.
Number display (5)
avoidScientificNotation

boolean. Default value: false. Whether to render numbers in full decimal form rather than scientific notation.

displayDecimals

integer. Default value: 2. Number of decimal places to display when rendering this number.

displayDigits

integer. Default value: 3. Number of significant digits to display when rendering this number.

displaySmallAsZero

number. Default value: 1e-14. Threshold below which numbers are displayed as zero.

padZeros

boolean. Default value: false. 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

boolean. 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 (2)
$s.allowUnsortedIn development

boolean. Report where each target belongs among the values in ascending order, whatever order they are written in.

$s.side

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

Number display (5)
$s.avoidScientificNotationIn development

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

$s.displayDecimalsIn development

integer. Number of decimal places to display when rendering this number.

$s.displayDigitsIn development

integer. Number of significant digits to display when rendering this number.

$s.displaySmallAsZeroIn development

number. Threshold below which numbers are displayed as zero.

$s.padZerosIn development

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

Common to all components (8)
$s.disabledIn development

boolean. Whether this component is disabled and cannot be interacted with.

$s.doenetML

text. The DoenetML source code that produced this component.

$s.fixedIn development

boolean. Whether this component's value is fixed and cannot be modified.

$s.fixLocationIn development

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

$s.hiddenIn development

boolean. Whether this component is hidden from the rendered output.

$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: 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.

Last updated on