R: Sort - Integer Sort - Sort a vector coresponding to another

Sort - Integer Sort - Sort a vector coresponding to another {Rfast} | R Documentation

## Sort - Integer Sort - Sort a vector coresponding to another

### Description

Fast sorting a vector.

### Usage

    Sort(x,descending=FALSE,partial=NULL,stable=FALSE,na.last=NULL)
    Sort.int(x)
    sort_cor_vectors(x, base, stable = FALSE, descending = FALSE)

### Arguments

`x` | A numerical/integer/character vector with data.
`base` | A numerical/character vector to help sorting the x.
`descending` | A boolean value (TRUE/FALSE) for sorting the vector in descending order. By default sorts the vector in ascending.
`partial` | This argument has two usages. The first is an index number for sorting partial the vector. The second is a vector with 2 values, start and end c(start,end). Gives you a vector where the elements between start and end will be sorted only. Not character vector.
`stable` | A boolean value (TRUE/FALSE) for choosing a stable sort algorithm. Stable means that discriminates on the same elements. Not character vector.
`na.last` | Accept 4 values. TRUE, FALSE, NA, NULL. TRUE/FALSE: for put NAs last or first. NA: for remove NAs completely from vector. NULL: by default. Leave it like that if there is no NA values.

### Details

This function uses the sorting algorithm from C++. The implementation is very fast and highly optimised. Especially for large data.

### Value

Sort and Sort.int: The sorted vector.

sort_cor_vectors: The first argument but sorted acording to the second.

### Author(s)

Manos Papadakis

R implementation and documentation: Manos Papadakis <papadakm95@gmail.com>.

### See Also

`nth, colnth, rownth,sort_unique, Round`

### Examples

    x <- rnorm(1000)
    system.time( s1 <- Sort(x) )
    system.time( s2 <- sort(x) )
    all.equal(s1,s2) #true  but not if many duplicates.

    system.time( s1 <- Sort(x,partial=100) )
    system.time( s2 <- sort(x,partial=100) )
    all.equal(s1,s2) #true

    system.time( s1 <- Sort(x,stable=TRUE) )
    system.time( s2 <- sort(x) )
    all.equal(s1,s2) #true

    x <- as.character(x)
    system.time( s1 <- Sort(x) )
    system.time( s2 <- sort(x) )
    all.equal(s1,s2) #true

    y <- runif(1000)
    b <- sort_cor_vectors(x,y)

    x<-rpois(100,100)
    all.equal(Sort.int(x),sort.int(x))

    x<-y<-y<-s1<-s2<-NULL

[Package Rfast version 2.0.0 Index]