How to sort list by first element

后端 未结 3 1584
鱼传尺愫
鱼传尺愫 2021-01-18 02:41

I have a list of vectors looking like:

[[1]]
[1] 2 1.0 3.0

[[2]]
[1] 3 3 3

[[3]]
[1] 1 3.0 1.0

and I want it to be sorted by first elemen

3条回答
  •  伪装坚强ぢ
    2021-01-18 03:15

    Similar to @MarkintheBox.

    L <- list(c(2,1,3), c(3,3,3), c(1,3,1))
    
    L[order(sapply(L,head,1),decreasing=T)]
    # [[1]]
    # [1] 3 3 3
    # 
    # [[2]]
    # [1] 2 1 3
    # 
    # [[3]]
    # [1] 1 3 1
    

提交回复
热议问题