Skip to content

Conformance Examples 0.1.0 — Transforms: transform-sort

sort-numbers

A list of numbers is sorted in ascending numeric order.

Template

{{ xs | sort }}

Context

{
    "xs": [
        3,
        1,
        2
    ]
}

Output

[1,2,3]

sort-strings

A list of strings is sorted in ascending lexicographic order.

Template

{{ xs | sort }}

Context

{
    "xs": [
        "banana",
        "apple",
        "cherry"
    ]
}

Output

["apple","banana","cherry"]

sort-by-numeric-property

With a property argument, structs are sorted by that numeric property.

Template

{{ items | sort: "n" }}

Context

{
    "items": [
        {
            "n": 3
        },
        {
            "n": 1
        },
        {
            "n": 2
        }
    ]
}

Output

[{"n":1},{"n":2},{"n":3}]

sort-by-string-property

With a property argument, structs are sorted by that string property.

Template

{{ items | sort: "k" }}

Context

{
    "items": [
        {
            "k": "b"
        },
        {
            "k": "a"
        }
    ]
}

Output

[{"k":"a"},{"k":"b"}]

sort-empty-list

Sorting an empty list yields an empty list.

Template

{{ xs | sort }}

Context

{
    "xs": []
}

Output

[]

sort-mixed-types-is-null

A list mixing numbers and strings is not uniformly comparable, so the result is null.

Template

{{ xs | sort }}

Context

{
    "xs": [
        1,
        "a",
        2
    ]
}

Output

null

sort-non-primitive-contents-is-null

Without a property, struct elements are not primitive keys, so the result is null.

Template

{{ xs | sort }}

Context

{
    "xs": [
        {
            "a": 1
        },
        {
            "a": 2
        }
    ]
}

Output

null

sort-property-non-struct-is-null

With a property, non-struct elements cannot supply the key, so the result is null.

Template

{{ xs | sort: "n" }}

Context

{
    "xs": [
        1,
        2
    ]
}

Output

null

sort-property-missing-is-null

With a property, a struct missing that property cannot supply the key, so the result is null.

Template

{{ items | sort: "n" }}

Context

{
    "items": [
        {
            "n": 1
        },
        {
            "m": 2
        }
    ]
}

Output

null

sort-non-list-is-null

sort operates only on lists; a string input yields null.

Template

{{ x | sort }}

Context

{
    "x": "hi"
}

Output

null

sort-strings-code-point-order

String ordering is by Unicode code point, so a BMP private-use character sorts before an astral character even though UTF-16 code-unit order would put the astral character's leading surrogate first.

Template

{{ xs | sort }}

Context

{
    "xs": [
        "😀",
        ""
    ]
}

Output

["","😀"]

Source fixture: transforms/transform-sort.json