Skip to content

Commit 8e88622

Browse files
committed
only apply sort if user set one
1 parent 3a1efb3 commit 8e88622

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

R/search-pv.R

+13-12
Original file line numberDiff line numberDiff line change
@@ -347,19 +347,20 @@ search_pv <- function(query,
347347
arg_list <- to_arglist(fields, size, primary_sort_key, after)
348348
paged_data <- request_apply(result, method, query, base_url, arg_list, api_key, ...)
349349

350-
# apply the user's sort using order()
350+
# we apply the user's sort, if they supplied one, using order()
351351
# was data.table::setorderv(paged_data, names(sort), ifelse(as.vector(sort) == "asc", 1, -1))
352-
353-
sort_order <- mapply(function(col, direction) {
354-
if (direction == "asc") {
355-
return(paged_data[[col]])
356-
} else {
357-
return(-rank(paged_data[[col]], ties.method = "min")) # Invert for descending order
358-
}
359-
}, col = names(sort), direction = as.vector(sort), SIMPLIFY = FALSE)
360-
361-
# Final sorting
362-
paged_data <- paged_data[do.call(order, sort_order), , drop = FALSE]
352+
if (!is.null(sort)) {
353+
sort_order <- mapply(function(col, direction) {
354+
if (direction == "asc") {
355+
return(paged_data[[col]])
356+
} else {
357+
return(-rank(paged_data[[col]], ties.method = "min")) # Invert for descending order
358+
}
359+
}, col = names(sort), direction = as.vector(sort), SIMPLIFY = FALSE)
360+
361+
# Final sorting
362+
paged_data <- paged_data[do.call(order, sort_order), , drop = FALSE]
363+
}
363364

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

0 commit comments

Comments
 (0)