R中列表的排序列表:排序列表的值取决于其他列表的值

我需要一些帮助来排序列表.假设我有一个如下的孩子列表:

a <- list(name = "Ann", age = 9)
b <- list(name = "Bobby", age = 17)
c <- list(name = "Alex", age = 6)

my.list <- list(a, b, c)

我想根据他们的年龄对他们的名字进行排序,所以收到以下信息:

> "Alex" "Ann" "Bobby"
最佳答案
a <- list(name = "Ann", age = 9)
b <- list(name = "Bobby", age = 17)
c <- list(name = "Alex", age = 6)

L <- list(a,b,c)
ages <- sapply(L,"[[","age")
names <- sapply(L,"[[","name")
names[order(ages)]

转载注明原文:R中列表的排序列表:排序列表的值取决于其他列表的值 - 代码日志