licata1996
licata1996

Reputation: 45

Sort list by values preserving names in R

Consider a list l like:

l<-list(a=24,b=12,c=30,d=1)

how to get the sorted version on values of such list, preserving names?

In result list the order of elements should be then: d,b,a and c corresponding to the sequence 1,12,24,30.

Upvotes: 3

Views: 1821

Answers (1)

akrun
akrun

Reputation: 887691

You can use order. Assuming that the length of each list element is 1 as showed in the example

l[order(unlist(l))]

Upvotes: 5

Related Questions