Sort strings alphabetically

My data looks like

ABCD BCAD XYMZ OMLP 

How can I sort the rows to get something like

 ABCD ABCD MXYZ LMOP 

Thanks,

+4
source share
5 answers

It’s good if you use Python and have your data in a list of lists, for example

 my_data = [ [ A, B, C, D ], [ B, C, A, D ], [ X, Y, M, Z ], [ O, M, L, P ] ] 

you could just do a simple list comprehension like this:

 sorted_lists = [sorted(l) for l in my_data] 

But since you did not specify any language or any other useful information, I do not know if this will help.

+1
source
 t(apply(DF, 1, sort)) 

The t() function is necessary because row operations on the apply family of functions return the results in column order.

+16
source

What have you tried? It is very simple and easy to solve with a simple loop.

 > s <- x > for(i in 1:NROW(x)) { + s[i,] <- sort(s[i,]) + } > s V1 V2 V3 V4 1 ABCD 2 ABCD 3 MXYZ 4 LMOP 
+5
source

No plyr answer yet ?!

 foo <- matrix(sample(LETTERS,10^2,T),10,10) library("plyr") aaply(foo,1,sort) 

Exactly the same as DWins answer, except you don't need t()

+2
source

And, if you want to sort in descending order by defining a function

  mysort <- function(x){ sort(x, decreasing = TRUE) } 

does the trick:

  t(apply(vot, 1, FUN = function(x) mysort(x))) 
0
source

All Articles