

If initialized is false (the default), then ix is initialized to contain the indices of v. If the index vector ix is initialized with the indices of v (or a permutation thereof), initialized should be set to true. Like partialsortperm, but accepts a preallocated index vector ix the same size as v, which is used to store (a permutation of) the indices of v. Partialsortperm!(ix, v, k by=, lt=, rev=false, initialized=false) Julia> sortslices(reshape(, (1,1,5)), dims=3, by=x->x)ĥ source Order-Related Functions Base.issorted - Function Higher dimensional examples julia> A = permutedims(reshape(, (2, 2, 2)), (1, 3, 2)) If dims is (2, 1) instead, the same slices will be taken, but the result order will be row-major instead. E.g., if A is three dimensional and dims is (1, 2), the orderings of the first two dimensions are re-arranged such that the slices (of the remaining third dimension) are sorted. If dims is a tuple, the order of the dimensions in dims is relevant and specifies the linear order of the slices. Note that while there is no default order on higher-dimensional slices, you may use the by or lt keyword argument to specify such an order. E.g., if A is a a 2x2x2 array, sortslices(A, dims=3) will sort slices within the 3rd dimension, passing the 2x2 slices A and A to the comparison function. Sortslices extends naturally to higher dimensions. Julia> sortslices(, dims=2, alg=InsertionSort, lt=(x,y)->isless(x,y)) Julia> sortslices(, dims=2) # Sort columns Julia> sortslices(, dims=1, lt=(x,y)->isless(x,y)) Note that the default comparison function on one dimensional slices sorts lexicographically.įor the remaining keyword arguments, see the documentation of sort!.Įxamples julia> sortslices(, dims=1) # Sort rows It specifies the dimension(s) over which the slices are sorted.Į.g., if A is a matrix, dims=1 will sort rows, dims=2 will sort columns. The required keyword argument dims must be either an integer or a tuple of integers.

Sortslices(A dims, alg::Algorithm=DEFAULT_UNSTABLE, lt=isless, by=identity, rev::Bool=false, order::Ordering=Forward) These options are independent and can be used together in all possible combinations: if both by and lt are specified, the lt function is applied to the result of the by function rev=true reverses whatever ordering specified via the by and lt keywords.Įxamples julia> v = sort!(v) v The by keyword lets you provide a function that will be applied to each element before comparison the lt keyword allows providing a custom "less than" function (note that for every x and y, only one of lt(x,y) and lt(y,x) can return true) use rev=true to reverse the sorting order. You can specify an algorithm to use via the alg keyword (see Sorting Algorithms for available algorithms). QuickSort is used by default for numeric arrays while MergeSort is used for other arrays. Sort!(v alg::Algorithm=defalg(v), lt=isless, by=identity, rev::Bool=false, order::Ordering=Forward) The isless function is invoked by default, but the relation can be specified via the lt keyword. If needed, the sorting algorithm can be chosen: julia> sort(v, alg=InsertionSort)Īll the sorting and order related functions rely on a "less than" relation defining a total order on the values to be manipulated. Or in reverse order by a transformation: julia> sort(v, by=abs, rev=true) Instead of directly sorting an array, you can compute a permutation of the array's indices that puts the array into sorted order: julia> v = randn(5)Īrrays can easily be sorted according to an arbitrary transformation of their values: julia> sort(v, by=abs) To sort an array in-place, use the "bang" version of the sort function: julia> a =

You can easily sort in reverse order as well: julia> sort(, rev=true) By default, Julia picks reasonable algorithms and sorts in standard ascending order: julia> sort() Julia has an extensive, flexible API for sorting and interacting with already-sorted arrays of values. Edit on GitHub Sorting and Related Functions
#Sort vector 2d code#
