rlist package provides functions for sorting list members by lambda expression.
devs <-
list(
p1=list(name="Ken",age=24,
interest=c("reading","music","movies"),
lang=list(r=2,csharp=4,python=3)),
p2=list(name="James",age=25,
interest=c("sports","music"),
lang=list(r=3,java=2,cpp=5)),
p3=list(name="Penny",age=24,
interest=c("movies","reading"),
lang=list(r=1,cpp=4,python=2)))
list.order
evaluates the given lambda expressions and find out the order by default ascendingly. If the values for some members tie, the next values of the next expression, if any, will count.
To adjust the order, use desc
function or simple write a minus operator (-
) before the expression.
list.order(devs, age)
# [1] 1 3 2
list.order(devs, length(interest))
# [1] 2 3 1
list.order(devs, desc(lang$r))
# [1] 2 1 3
list.order(devs, max(unlist(lang)))
# [1] 1 3 2
list.order(devs, desc(length(interest)), desc(lang$r))
# [1] 1 2 3
list.sort
produces a sorted list of the original list members. Its usage is exactly the same as list.order
.
str(list.sort(devs, age))
# List of 3
# $ p1:List of 4
# ..$ name : chr "Ken"
# ..$ age : num 24
# ..$ interest: chr [1:3] "reading" "music" "movies"
# ..$ lang :List of 3
# .. ..$ r : num 2
# .. ..$ csharp: num 4
# .. ..$ python: num 3
# $ p3:List of 4
# ..$ name : chr "Penny"
# ..$ age : num 24
# ..$ interest: chr [1:2] "movies" "reading"
# ..$ lang :List of 3
# .. ..$ r : num 1
# .. ..$ cpp : num 4
# .. ..$ python: num 2
# $ p2:List of 4
# ..$ name : chr "James"
# ..$ age : num 25
# ..$ interest: chr [1:2] "sports" "music"
# ..$ lang :List of 3
# .. ..$ r : num 3
# .. ..$ java: num 2
# .. ..$ cpp : num 5
str(list.sort(devs, length(interest)))
# List of 3
# $ p2:List of 4
# ..$ name : chr "James"
# ..$ age : num 25
# ..$ interest: chr [1:2] "sports" "music"
# ..$ lang :List of 3
# .. ..$ r : num 3
# .. ..$ java: num 2
# .. ..$ cpp : num 5
# $ p3:List of 4
# ..$ name : chr "Penny"
# ..$ age : num 24
# ..$ interest: chr [1:2] "movies" "reading"
# ..$ lang :List of 3
# .. ..$ r : num 1
# .. ..$ cpp : num 4
# .. ..$ python: num 2
# $ p1:List of 4
# ..$ name : chr "Ken"
# ..$ age : num 24
# ..$ interest: chr [1:3] "reading" "music" "movies"
# ..$ lang :List of 3
# .. ..$ r : num 2
# .. ..$ csharp: num 4
# .. ..$ python: num 3
str(list.sort(devs, desc(lang$r)))
# List of 3
# $ p2:List of 4
# ..$ name : chr "James"
# ..$ age : num 25
# ..$ interest: chr [1:2] "sports" "music"
# ..$ lang :List of 3
# .. ..$ r : num 3
# .. ..$ java: num 2
# .. ..$ cpp : num 5
# $ p1:List of 4
# ..$ name : chr "Ken"
# ..$ age : num 24
# ..$ interest: chr [1:3] "reading" "music" "movies"
# ..$ lang :List of 3
# .. ..$ r : num 2
# .. ..$ csharp: num 4
# .. ..$ python: num 3
# $ p3:List of 4
# ..$ name : chr "Penny"
# ..$ age : num 24
# ..$ interest: chr [1:2] "movies" "reading"
# ..$ lang :List of 3
# .. ..$ r : num 1
# .. ..$ cpp : num 4
# .. ..$ python: num 2
str(list.sort(devs, max(unlist(lang))))
# List of 3
# $ p1:List of 4
# ..$ name : chr "Ken"
# ..$ age : num 24
# ..$ interest: chr [1:3] "reading" "music" "movies"
# ..$ lang :List of 3
# .. ..$ r : num 2
# .. ..$ csharp: num 4
# .. ..$ python: num 3
# $ p3:List of 4
# ..$ name : chr "Penny"
# ..$ age : num 24
# ..$ interest: chr [1:2] "movies" "reading"
# ..$ lang :List of 3
# .. ..$ r : num 1
# .. ..$ cpp : num 4
# .. ..$ python: num 2
# $ p2:List of 4
# ..$ name : chr "James"
# ..$ age : num 25
# ..$ interest: chr [1:2] "sports" "music"
# ..$ lang :List of 3
# .. ..$ r : num 3
# .. ..$ java: num 2
# .. ..$ cpp : num 5
str(list.sort(devs, desc(length(interest)), desc(lang$r)))
# List of 3
# $ p1:List of 4
# ..$ name : chr "Ken"
# ..$ age : num 24
# ..$ interest: chr [1:3] "reading" "music" "movies"
# ..$ lang :List of 3
# .. ..$ r : num 2
# .. ..$ csharp: num 4
# .. ..$ python: num 3
# $ p2:List of 4
# ..$ name : chr "James"
# ..$ age : num 25
# ..$ interest: chr [1:2] "sports" "music"
# ..$ lang :List of 3
# .. ..$ r : num 3
# .. ..$ java: num 2
# .. ..$ cpp : num 5
# $ p3:List of 4
# ..$ name : chr "Penny"
# ..$ age : num 24
# ..$ interest: chr [1:2] "movies" "reading"
# ..$ lang :List of 3
# .. ..$ r : num 1
# .. ..$ cpp : num 4
# .. ..$ python: num 2