issorted

issorted(v, [by=,] [lt=,] [rev=false])

Test whether a vector is in sorted order. The by, lt and rev keywords modify what order is considered to be sorted just as they do for sort.

Examples

Check if a vector is sorted in ascending order:

julia> v = [1, 2, 3, 4];
julia> issorted(v)
true

This example checks if the vector v is sorted in ascending order. Since it is sorted, the function returns true.

Check if a vector is sorted in descending order:

julia> v = [4, 3, 2, 1];
julia> issorted(v, rev=true)
true

Here, the rev=true argument is used to check if the vector v is sorted in descending order. The function returns true because the vector is indeed sorted in descending order.

Check if a vector of strings is sorted based on string length:

julia> v = ["apple", "banana", "orange", "grape"];
julia> issorted(v, by=length)
true

In this example, the by=length argument is used to check if the vector of strings v is sorted based on string length. Since the strings are sorted in increasing order of length, the function returns true.

Check if a vector is sorted using a custom comparison function:

julia> v = [2.5, 6.7, 4.3, 9.1];
julia> issorted(v, lt=(x, y)->abs(x) < abs(y))
false

Here, the lt=(x, y)->abs(x) < abs(y) argument is used to define a custom comparison function. The function checks if the absolute values of elements in the vector v are sorted in ascending order. Since the vector is not sorted in this manner, the function returns false.

Common mistake example:

julia> v = [3, 1, 2];
julia> issorted(v, by=length)
ERROR: MethodError: no method matching length(::Int64)

In this example, the by=length argument is used with the issorted function, expecting to sort a vector of integers based on their length. However, the length function is not applicable to integers, resulting in a MethodError. It's important to provide appropriate arguments based on the type of elements in the vector.

See Also

digits, inf, isdigit, iseven, isfinite, isless, islower, isnumber, isodd, isprime, isqrt, issorted, issubnormal, isxdigit, nan,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: