Skip to content

Commit

Permalink
only apply sort if user set one
Browse files Browse the repository at this point in the history
  • Loading branch information
mustberuss committed Dec 25, 2024
1 parent 3a1efb3 commit 8e88622
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions R/search-pv.R
Original file line number Diff line number Diff line change
Expand Up @@ -347,19 +347,20 @@ search_pv <- function(query,
arg_list <- to_arglist(fields, size, primary_sort_key, after)
paged_data <- request_apply(result, method, query, base_url, arg_list, api_key, ...)

# apply the user's sort using order()
# we apply the user's sort, if they supplied one, using order()
# was data.table::setorderv(paged_data, names(sort), ifelse(as.vector(sort) == "asc", 1, -1))

sort_order <- mapply(function(col, direction) {
if (direction == "asc") {
return(paged_data[[col]])
} else {
return(-rank(paged_data[[col]], ties.method = "min")) # Invert for descending order
}
}, col = names(sort), direction = as.vector(sort), SIMPLIFY = FALSE)

# Final sorting
paged_data <- paged_data[do.call(order, sort_order), , drop = FALSE]
if (!is.null(sort)) {
sort_order <- mapply(function(col, direction) {
if (direction == "asc") {
return(paged_data[[col]])
} else {
return(-rank(paged_data[[col]], ties.method = "min")) # Invert for descending order
}
}, col = names(sort), direction = as.vector(sort), SIMPLIFY = FALSE)

# Final sorting
paged_data <- paged_data[do.call(order, sort_order), , drop = FALSE]
}

# remove the fields we added in order to do the user's sort ourselves
paged_data <- paged_data[, !names(paged_data) %in% additional_fields]
Expand Down

0 comments on commit 8e88622

Please sign in to comment.