Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created January 13, 2014 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramnathv/8393419 to your computer and use it in GitHub Desktop.
Save ramnathv/8393419 to your computer and use it in GitHub Desktop.
Sort a List based on a Key
#' Sort a list based on a key
#'
#' @example
#' x = list(
#' list(a = 1, b = 4),
#' list(a = 3, b = 3),
#' list(a = 2, b = 2)
#' )
sort_list <- function(x, key, ...){
y = sapply(x, '[[', key)
y2 = sort(y, index.return = TRUE, ...)$ix
x[y2]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment