## How to sort list of strings by custom rules in R?

string matching in r
r partial string match
r extract string between characters
create custom sort list excel mac
r extract string after character
r find character in string
r subset partial string match
string r

I want use R to sort a list of `c("ABC-L1","DEF-L2","XYZ-B12","AAA-B1")`, in a order that with `/-([LB])(\d+)/`, first by `\$1`, and then by `\$2`.

Which means, I need the order `c("AAA-B1","XYZ-B12","ABC-L1","DEF-L2")`.

How to do this ?

We can use `sub`:

```vec <- c("ABC-L1","DEF-L2","XYZ-B12","AAA-B1")

vec[order(sub('^[A-Z]+-', '', vec))]

#  "AAA-B1"  "XYZ-B12" "ABC-L1"  "DEF-L2"
```

`sub` removes everything upto `-`, we then `order` the rest.

14 Strings, You can pick which set of rules to use by specifying a locale: # Turkish The base R order() and sort() functions sort strings using the current locale. If you want  For example, to get R to sort the alphabet in reverse, use the sort() function: Here you used the decreasing argument of sort(). The sort() function sorts a vector. It doesn’t sort the characters of each element of the vector. In other words, sort() doesn’t mangle the word itself.

The following does what you want.

```x <- c("ABC-L1","DEF-L2","XYZ-B12","AAA-B1")

sp <- do.call(rbind, strsplit(x, "-"))
inx <- order(sp[, 2], sp[, 1])

x[inx]
# "AAA-B1"  "XYZ-B12" "ABC-L1"  "DEF-L2"
```

Sort data using a custom list - Excel - office support, Optionally, you can create your own custom lists to sort by any other characteristic that doesn't sort well alphabetically—such as high, medium, and low—or S, M, L,  R makes it easy to sort vectors in either ascending or descending order. Because each column of a data frame is a vector, you may find that you perform this operation quite frequently. How to sort a vector in ascending order. To sort a vector, you use the sort () function. For example, to sort Population in ascending order, try this:

use str_plit to separate by "-" and then order by columns

```  text <- c("ABC-L1","DEF-L2","XYZ-B12","AAA-B1")

text<- t(as.data.frame(str_split(text, "-")))
res <- text[order(text[, 2], text[, 1]),]
```

Sort an array according to the order defined by another array , Given two arrays A1[] and A2[], sort A1 in such a way that the relative order among the elements will be same as those are in A2. For the elements not present in A2, append them at last in sorted order. Example: public static void main(String args[]) A custom comapre method to compare elements of A1[] according. sort is a generic function for which methods can be written, and sort.int is the internal method which is compatible with S if only the first three arguments are used. The default sort method makes use of order for classed objects, which in turn makes use of the generic function xtfrm (and can be slow unless a xtfrm method has been defined or is.numeric (x) is true).

How to sort Umlaute in Java correctly?, Have you ever had to sort Strings in Java, e.g. to show documents and files Imagine, we have following list of strings: J<k,K<l,L<m,M<n,N<o,O<p,P<q,Q<r,​R<s, S & SS,ß<t,T& TH, Þ &TH,"+ There are builtin collator rules for various languages, so you can explore them or adapt them with the custom rules to your need. For sort.int the value is the sorted vector unless index.return is true, when the result is a list with components named x and ix containing the sorted numbers and the ordering index vector. In the latter case, if method == "quick" ties may be reversed in the ordering (unlike sort.list ) as quicksort is not stable.

Sophisticated Sorting in JavaScript, The specification defines the rules in a confusing way But what this actually means is, “sort b lower down the list than a ”, which in terms of numerical we can directly compare the strings like that, to then return one of the three sorting values: var letters = ["R","O","F","L"]; letters.sort(function(a, b) { var x = a. Use the overload of OrderBy that takes an IComparer, the first Func argument will feed the comparer, and from there you need to compare the strings. First deal with the case of one or both starts with _, and then from there you will probably need to strip the _ and just use the standard string.Compare to sort them beyond the first _

How to Sort a List in Java, Let's look at a quick example to sort a list of strings. package com.journaldev.sort; import java.util.ArrayList; import java.util.Collections; import java.util. cmp specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.