sort
Sort into Numeric or Alphabetic Order

### Description

Returns the input vector in proper sort order.

### Usage

    sort(x, decreasing = FALSE, ...)
    sort.int(x, partial = NULL, na.last = NA, decreasing = FALSE,
         method = c("shell", "quick"), index.return = FALSE)
    ## Default S3 method:
    sort(x, decreasing = FALSE, na.last = NA, ...)
    ## S3 method for class 'POSIXlt':
    sort(x, decreasing = FALSE, na.last = NA, ...)

### Arguments

x | a numeric or character vector. Infinite values (Inf) are allowed in numeric vectors. Missing values (NAs) are allowed in the input vector, but the default behavior deletes any missing values.
decreasing | a logical value. If TRUE, the elements have a descending sort order. If FALSE (the default), the elements have an ascending order.
partial | a vector of indices into data for partial sorting. If partial has a positive length, only the elements named in partial are guaranteed to be in their proper positions after the sort. All other elements are in their proper gaps between the partial elements, but not necessarily in the proper order within the gaps.
na.last | a vector of length 1 that specifies how to handle missing values (NAs). The possible values and results are: na.last = NA (the default). Missing values are deleted. In this case, the resulting sorted vector contains fewer elements than the input. na.last = TRUE. Missing values are placed at the end of the sorted vector. na.last = FALSE. Missing values are placed at the beginning of the sorted vector.
method | specifies the sort algorithm. By default, when index.return = TRUE, the vector is sorted using a stable sort algorithm. If you specify method = "quick", it uses an unstable (and faster) sort algorithm. If you specify any other method, the default sort algorithm is employed. In particular, the "shell" method is present only for R compatibility, and does not imply using shell sort.
index.return | a logical value. If TRUE, the return value is a list that contains both the sorted vector and an ordering vector. By default, when index.return = TRUE the input vector is sorted using a stable sort algorithm , however, if you specify method = "quick" then an unstable (and faster) sort algorithm is used.
... | additional arguments passed to the generic method.

### Details

Any attributes of x, except names, are lost in the sort operation.
Character data are sorted according to the ASCII collating sequence, where digits precede upper-case letters, which precede lower-case letters; the position of other characters is not intuitive. Complex data are sorted according to the real part, and the imaginary part is used to break ties.
By default, sorting of non-atomic data is not allowed, because the definition of ordering is too vague. However, for classed data, the generic xtfrm function is called to provide an ordering. If it succeeds, the sort also succeeds.
To sort a series object x by selected data columns, use order or rank.
Value
returns a vector.

- If index.return = FALSE, the result is a sorted vector.
- If index.return = TRUE, the resut is a list where the first element "x" is a sorted vector and the second element "ix" is an ordering vector.

Differences between Spotfire Enterprise Runtime for R and Open-source R

- Open-source R handles only index.return=TRUE when na.last=NA. Spotfire Enterprise Runtime for R is more flexible: sort(mydata, na.last = TRUE, index.return = TRUE) # Error: "index.return'"only for "na.last = NA"

- The sort algorithms are not the same. The result however, should be the same unless partial is used (in which case the sort order is undefined for the ranges in between the specified partial indices).

See Also
sort.list, order, rank, rev, xtfrm, POSIXt.
Examples

    mydata <- c(2, 9, 18, NA, 3, 2) # create sample object
    sort(mydata) # missing values are deleted
    sort(mydata, na.last = TRUE) # put missing values at the end
    sort(mydata, index.return = TRUE)
    sort(sample(1:100, 50), decreasing = TRUE, method = "quick")
    sort(sample(1:100, 50), partial = c(20, 30))
    ## POSIXlt
    (plt <- as.POSIXlt("1970-1-1") + c(NA, 1:3))
    sort(plt, decreasing = TRUE, na.last = TRUE)

Package base version 6.1.5-18
Package Index