diff --git a/packages/db-postgres/src/queries/buildQuery.ts b/packages/db-postgres/src/queries/buildQuery.ts index 139dfe7cf4e..a6120a3b3ff 100644 --- a/packages/db-postgres/src/queries/buildQuery.ts +++ b/packages/db-postgres/src/queries/buildQuery.ts @@ -52,29 +52,32 @@ const buildQuery = async function buildQuery({ let orderBy: Result['orderBy'] = [] if (sort) { - orderBy = sort.split(',').map((sortString) => { - const sortPath = sortString.replace(/^-/, '') - try { - const { columnName: sortTableColumnName, table: sortTable } = getTableColumnFromPath({ - adapter, - collectionPath: sortPath, - fields, - joinAliases, - joins, - locale, - pathSegments: sortPath.replace(/__/g, '.').split('.'), - selectFields, - tableName, - value: sortPath, - }) - return { - column: sortTable?.[sortTableColumnName] ?? null, - order: sortString[0] === '-' ? desc : asc, + orderBy = sort + .split(',') + .map((sortString) => { + const sortPath = sortString.replace(/^-/, '') + try { + const { columnName: sortTableColumnName, table: sortTable } = getTableColumnFromPath({ + adapter, + collectionPath: sortPath, + fields, + joinAliases, + joins, + locale, + pathSegments: sortPath.replace(/__/g, '.').split('.'), + selectFields, + tableName, + value: sortPath, + }) + return { + column: sortTable?.[sortTableColumnName] ?? null, + order: sortString[0] === '-' ? desc : asc, + } + } catch (err) { + // continue } - } catch (err) { - // continue - } - }) + }) + .filter((sortInfo) => !!sortInfo) } if (!orderBy.length) { diff --git a/packages/payload/src/versions/drafts/getQueryDraftsSort.ts b/packages/payload/src/versions/drafts/getQueryDraftsSort.ts index 3da4ec9350e..64b07fda971 100644 --- a/packages/payload/src/versions/drafts/getQueryDraftsSort.ts +++ b/packages/payload/src/versions/drafts/getQueryDraftsSort.ts @@ -5,13 +5,16 @@ export const getQueryDraftsSort = (sort: string): string => { if (!sort) return sort - let direction = '' - let orderBy = sort - - if (sort[0] === '-') { - direction = '-' - orderBy = sort.substring(1) - } - - return `${direction}version.${orderBy}` + return sort + .split(' ') + .map((sortString) => { + let direction = '' + let orderBy = sortString + if (sortString[0] === '-') { + direction = '-' + orderBy = sortString.substring(1) + } + return `${direction}version.${orderBy}` + }) + .join(' ') }