Github user sun-rui commented on a diff in the pull request: https://github.com/apache/spark/pull/8920#discussion_r40746150 --- Diff: R/pkg/R/DataFrame.R --- @@ -1848,3 +1848,62 @@ setMethod("crosstab", sct <- callJMethod(statFunctions, "crosstab", col1, col2) collect(dataFrame(sct)) }) + +#' Sort +#' +#' Sort a DataFrame by the specified column(s). +#' +#' @param x A DataFrame to be sorted. +#' @param by A character column indicating the field to sort on. +#' if the column names are specified as Column object arrange function +#' can be used instead +#' @param decreasing Orderings for each sorting column +#' @param ... Additional sorting fields +#' @return A DataFrame where elements are sorted by input sorting columns. +#' @rdname sort +#' @name sort +#' @aliases orderby +#' @export +#' @examples +#'\dontrun{ +#' sc <- sparkR.init() +#' sqlContext <- sparkRSQL.init(sc) +#' path <- "path/to/file.json" +#' df <- jsonFile(sqlContext, path) +#' sort(df, col="col1") +#' sort(df, decreasing=FALSE, "col2") +#' sort(df, decreasing=TRUE, "col1") +#' sort(df, c(TRUE,FALSE), "col1","col2") +#' } +setMethod("sort", + signature(x = "DataFrame"), + function(x, decreasing=FALSE, col, ...) { + + # all sorting columns + by <- c(col, ...) + if (length(decreasing) == 1){ + # in case only 1 boolean argument - decreasing value is specified, + # it will be used for all columns + decreasing <- rep(decreasing,length(by)) + } else if (length(decreasing) != length(by)){ + stop("Arguments 'col' and 'decreasing' must have the same length") + } + + # builds a list of columns of type Column + # example: [[1]] Column Species ASC + # [[2]] Column Petal_Length DESC + columns <- lapply(seq_len(length(decreasing)), function(i){ + if (decreasing[[i]]){ + desc(getColumn(x, by[[i]])) + }else{ + asc(getColumn(x, by[[i]])) + } + }) + --- End diff -- The following logic can be replaced by: do.call("arrange", x, columns)
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org