## Sort a vector using a "personalised" order in R

I want to sort a character vector called c:

c<-c("AD 2017", "AD 2018 ","RT 2017","BL 2017","BL 2018","CT 2018"

If I use R's built in function sort, this is what I get:

> sort(c) [1] "AD 2017" "AD 2018 " "BL 2017" "BL 2018" "CT 2018" "RT 2017"

However, let's say I have a different ordering system for the values, which is kept in a matrix and looks like this:

ORDER VALUE 1 1 RT 2 2 BL 3 3 AD 4 4 CT

The question is how can I sort my "c" vector so it uses the order from the matrix, all while taking into account the different years; my "custom" sorted vector should look like this:

> special_sort(c) [1] "RT 2017" , "BL 2017" , "BL 2018", "AD 2017" , "AD 2018 " , "CT 2018"

I really need to find a way to automate this, as my database is quite large.

Thank you in advance for your help

Another option could be:

x[order(match(substr(x, 1, 2), df$VALUE))] [1] "RT 2017" "BL 2017" "BL 2018" "AD 2017" "AD 2018 " "CT 2018"

Sample data:

x <- c("AD 2017", "AD 2018 ","RT 2017","BL 2017","BL 2018","CT 2018") df <- read.table(text = " ORDER VALUE 1 1 RT 2 2 BL 3 3 AD 4 4 CT", header = TRUE, stringsAsFactors = FALSE)

**Sorting a vector in C++,** Sorting a vector in C++ Sorting a vector in C++ can be done by using std::sort(). It is defined in<algorithm> header. To get a stable sort std::stable_sort is used. It is exactly like sort() but maintains the relative order of equal elements. Sorting a vector in C++ can be done by using std::sort(). It is defined in<algorithm> header. To get a stable sort std::stable_sort is used. It is exactly like sort() but maintains the relative order of equal elements. Quicksort(), mergesort() can also be used, as per requirement. Algorithm Begin Decalre v of vector type.

You can try something like this:

# order it by the first two characters, using the levels of factor choosen v[order(factor(substr(v,1,2),levels = c("RT","BL","AD","CT")))] [1] "RT 2017" "BL 2017" "BL 2018" "AD 2017" "AD 2018 " "CT 2018"

So with a matrix:

# use the second column of the matrix in unique(), to order v[order(factor(substr(v,1,2),levels = unique(mat[,2])))] [1] "RT 2017" "BL 2017" "BL 2018" "AD 2017" "AD 2018 " "CT 2018"

With vector and matrix:

# your vector v<-c("AD 2017", "AD 2018 ","RT 2017","BL 2017","BL 2018","CT 2018") # your matrix mat <- structure(c("1", "2", "3", "4", "RT", "BL", "AD", "CT"), .Dim = c(4L, 2L), .Dimnames = list(c("1", "2", "3", "4"), c("ORDER", "VALUE" )))

**std::sort,** In this post, we will discuss how to sort a vector of integers in C++ in ascending order.. The recommended approach is to use the standard algorithm std::sort� You can sort a vector of custom objects using the C++ STL function std::sort. The sort function has an overloaded form that takes as arguments first, last, comparator. The first and last are iterators to first and last elements of the container. The comparator is a predicate function that can be used to tell how to sort the container.

Not sure if you have `year`

also taken into account when encountering the same alphabet prefix. If you have, then the following can help you

res <- c[order( match(gsub("([[:alpha:]]+).*","\\1",v),df$VALUE), sort(as.numeric(gsub(".*?([[:digit:]]+)","\\1",v))))]

which gives

> res [1] "RT 2017" "BL 2017" "BL 2018" "AD 2017" [5] "AD 2018 " "CT 2018"

Otherwise, `c[order(match(gsub("([[:alpha:]]+).*","\\1",v),df$VALUE))]`

is enough for use if you only care about the order of `df$VALUE`

**DATA**:

df <- structure(list(ORDER = 1:4, VALUE = c("RT", "BL", "AD", "CT")), class = "data.frame", row.names = c("1", "2", "3", "4"))

**Sort a vector in C++,** To get the ball rolling, let's see how to sort a vector of integers. #include < iostream> // std::cout� Given a vector, sort this vector in descending order using STL in C++. Example: Input: vec = {1, 45, 54, 71, 76, 12} Output: {76, 71, 54, 45, 12, 1} Input: vec = {1

##### Data

vector <- c("AD 2017", "AD 2018 ","RT 2017","BL 2017","BL 2018","CT 2018")

##### Ordering Function

order_fun <- function(vector) { df <- data.frame(do.call(rbind, strsplit(vector, " "))) df$X1 <- factor(df$X1, levels = c("RT", "BL", "AD", "CT"), labels = c("RT", "BL", "AD", "CT")) df <- df[order(df$X1, df$X2), ] vector_ordered <- vector[as.numeric(row.names(df))] return(vector_ordered) }

##### Ordering one-liner

vector[order(factor(substr(vector,1,2), levels = c("RT", "BL", "AD", "CT")), substr(vector,4,7))]

##### Results

order_fun(vector) [1] "RT 2017" "BL 2017" "BL 2018" "AD 2017" "AD 2018 " "CT 2018"

**Sorting A Vector In C++,** Sort the integers and print the sorted order. Store the integers in a vector.Vectors are sequence containers representing arrays that can change in size. Here is a std::sort helper that enables to sort two vectors simultaneously, taking into account the values of only one vector. This solution is based on a custom RadomIt (random iterator), and operates directly on the original vector data, without temporary copies, structure rearrangement or additional indices:

**Vector-Sort,** std::sort(object.begin(), object.end(), [] (myclass const& a, myclass const& b) Or if you're stuck with C++03, the function object approach ( v is the member on� If A is a matrix, then sort (A) treats the columns of A as vectors and sorts each column. If A is a multidimensional array, then sort (A) operates along the first array dimension whose size does not equal 1, treating the elements as vectors. example. B = sort (A,dim) returns the sorted elements of A along dimension dim .

**How to sort an STL vector?,** The order of equal elements is not guaranteed to be preserved. A sequence is sorted with respect to a comparator comp if for any iterator it� Since the first parameter in sort () accepts a iterator (pointer) to the first element we want to sort we can pass stringVec.begin () to it because.begin () returns a iterator to the first element. So it will start the sorting at the first element in the vector.

**std::sort,** A vector in C++ can be easily sorted in ascending order using the sort() function defined in the algorithm header file. The sort() function sorts a given data structure� Case 1 : Sorting the vector elements on the basis of first element of pairs in ascending order. This type of sorting can be achieved using simple “ sort() ” function. By default the sort function sorts the vector elements on basis of first element of pairs.