Sorting a list by values ​​preserving names in R

Consider the list l

as:

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

      

how to get a sorted version on the values ​​of such a list while preserving the names?

In the results list, the order of elements should be as follows: d

, b

, a

and c

corresponding to the sequence 1,12,24,30.

+3


source to share


1 answer


You can use order

. Assuming the length of each list item is 1 as shown in the example



l[order(unlist(l))]

      

+4


source







All Articles