Skip to Content

<sort>

Sorts a list according to a comparison function

<sort> is a General Operator component that reorders a list of components based on given criteria.

Attributes and Properties

Attributes for <sort>

Highlighted (3)
asList

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

text. Name of a property to sort by (e.g. "x" for sorting points by x-coordinate).

keyword. Component type to sort bare string children as. Omit it and they are read as what they look like: every piece naming a number sorts by value, anything else sorts alphabetically.

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.
Sort order (3)

integer. Default value: 1. Index of the component to sort by (when sorting vectors).

text. Name of a property to sort by (e.g. "x" for sorting points by x-coordinate).

keyword. Whether to sort vectors by component or by magnitude.

ValueDescription
displacement (default)Sort vectors by their displacement components.
tailSort vectors by the position of their tail point.
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 <sort name="s">

Sort order (2)
$s.sortByComponent

integer. Index of the component to sort by (when sorting vectors).

$s.sortVectorsBy

text. Whether to sort vectors by component or by magnitude.

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: <sort> a list of numbers

In this example, <number> components are sorted in increasing order using the <sort> component.


Example: <sort> numbers from a random selection

In this example, randomly selected numbers generated with a <selectRandomNumbers> component are sorted in increasing order using the <sort> component.


Example: <sort> numbers from user-input

In this example, numbers entered by the user with a <mathInput> component are referenced by name and are sorted in increasing order using the <sort> component.


Example: Alphabetical sorting

In this example, names entered by the user with a <textInput> component are referenced by name and are sorted in alphabetical order using the <sort> component.

Attribute Examples

Attribute Example: type

Bare string children are read as what they look like: if every whitespace-separated piece names a number the list is numeric, and otherwise it is text. So <sort>10 2 1</sort> sorts by value and <sort>d a b</sort> sorts alphabetically with nothing declared either way, and most documents never need type at all. Write it for the cases where the look is misleading.

Numerals that are labels rather than quantities. Zip codes, course numbers and rankings are written with digits but are not amounts.

The first line drops the leading zeros that make a zip code a zip code. type="text" is also what puts 10 before 9, which is the order a list of labels usually wants.

Expressions that should be compared as maths. Two spellings of one expression are two different values as text and a single value as maths.

type="math" also keeps an expression in the form it was written while still ordering it by value: <sort type="math">1/2 2^3 pi sqrt(4)</sort> puts the four expressions in order as they were typed, where without type the same four arrive as 0.5, 2, 3.14, 8.

Booleans. Without type these are just words, and alphabetical order happens to put false first — so it looks right until the capitalization varies.

There is no case for writing type="number": a piece that names a number is already read as one, and one that does not becomes NaN. type applies only to bare strings — a referenced component carries a type of its own and keeps it.


Attribute Example: sortByComponent

For multi-dimensional object like points, sorting by sortByComponent="1" sorts by their first component, i.e., their xx-component, while using sortByComponent="2" results in sorting based on the yy-component.

Values entered outside the dimensions of the object, such as entering a 33 or a 00 in for a two-dimensional object, will result in no sorting.


Attribute Example: sortByProp

Components with numerical properties can be sorted based on an individual property. In this example, the points are sorted in ascending order based on their xx-coordinates, and then by their yy-coordinates.


Attribute Example: sortVectorsBy

Vectors can be sorted based on the location of their tail or by their displacement.

When sorted based on tail, Doenet defaults to sorting based on the xx-coordinate unless specified otherwise using sortByProp="y".

When sorted based on displacement, Doenet defaults to sorting based on the displacement in the xx-direction unless specified otherwise using sortByProp="x2", as in this example. This is because vector displacements are individually accessed using the convention: x1 for the horizontal displacement, and x2 for the vertical displacement.

Last updated on