Sort key, list of values ​​in R by value

Given the list of animals, name it m, which contains

$bob
[1] 3

$ryan
[1] 4

$dan
[1] 1

How can I sort this guy by numerical value? Basically, I would like my code to look like this:

m=sort(m,sortbynumber)


$ryan
[1] 4

$bob
[1] 3

$dan
[1] 1

I can’t figure it out, unfortunately. Sounds like a simple solution.

+4
source share
1 answer

You can try order

m[order(-unlist(m))]
#$ryan
#[1] 4

#$bob
#[1] 3

#$dan
#[1] 1

Or a slightly more efficient option would be to use an argument decreasing=TRUE order(from @nicola's comments)

m[order(unlist(m), decreasing=TRUE)]
+4
source

All Articles