Github user NarineK commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8920#discussion_r40732314
  
    --- Diff: R/pkg/R/DataFrame.R ---
    @@ -1848,3 +1848,78 @@ 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 or Column Object  indicating the field to 
sort on.
    +#'           If sorting column is a Column object, we need to embrace the 
column with asc or desc
    +#'           keyword. The 'decreasing' argument does not apply to Column 
Object. It only applies to
    +#'           character column names  
    +#' @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")
    +#' sort(df, col=list(asc(df$col1), desc(df$col2)))
    +#' sort(df, col=desc(df$col1))
    +#' }
    +setMethod("sort",
    +          signature(x = "DataFrame"),
    +          function(x, decreasing=FALSE, col, ...) {
    +
    +            # all sorting columns
    +            by <- c(col, ...)
    +
    +            if (class(by) == "character"){
    +              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")
    +              }
    +
    +              # creates a string array by replacing TRUE/FALSE 
correspondingly by "desc"/"asc"
    +              sortOrder <- ifelse (decreasing == FALSE, "asc", decreasing)
    +              sortOrder <- ifelse (decreasing == TRUE, "desc", sortOrder)
    +
    +              # concatenates dataframe with the column names, example: 
c("x$Species", "x$Petal_Width")
    +              colDFConcat <- paste("x", by, sep = "$")
    +
    +              # embraces columns with order - asc/desc
    +              # example: c("asc(x$Species)", "desc(x$Petal_Length)" )
    +              colDFOrderConcat <- paste(sortOrder, "(", colDFConcat, ")", 
collapse = ",")
    +
    +              # concatenates all ordered columns to a list
    +              # example: "list(asc(x$Species), desc(x$Petal_Length))"
    +              colDFOrderConcatList <- paste("list(", colDFOrderConcat, 
")", collapse = "")
    +
    +              # builds columns of type Column, example: [[1]] Column 
Species ASC
    +              #                                         [[2]] Column 
Petal_Length DESC
    +              resCols <- eval(parse(t=colDFOrderConcatList))
    --- End diff --
    
    I've made the changes with lapply ... . The code looks more elegant now. 
Thank you!
    
    Also, I couldn't directly call arrange, because I have a list of columns 
and it doesn't work when I pass a list or an array to arrange(x,col,...). I had 
to somehow cast it as three dot and it wasn't feasible. If you know how to do 
it and can share it with me that would be great.
    
    Let me know what do you think about my latest commit
    
    Thanks,
    Narine 


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to