Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import {getThreadReportIDsForTransactions} from '@libs/MoneyRequestReportUtils';
import {navigationRef} from '@libs/Navigation/Navigation';
import Parser from '@libs/Parser';
import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils';
import {getMoneyRequestSpendBreakdown} from '@libs/ReportUtils';
import {getMoneyRequestSpendBreakdown, isExpenseReport} from '@libs/ReportUtils';
import {compareValues, getColumnsToShow, isTransactionAmountTooLong, isTransactionTaxAmountTooLong} from '@libs/SearchUIUtils';
import {getTransactionPendingAction, isTransactionPendingDelete} from '@libs/TransactionUtils';
import {getAmount, getCategory, getCreated, getMerchant, getTag, getTransactionPendingAction, isTransactionPendingDelete} from '@libs/TransactionUtils';
import shouldShowTransactionYear from '@libs/TransactionUtils/shouldShowTransactionYear';
import Navigation from '@navigation/Navigation';
import type {ReportsSplitNavigatorParamList} from '@navigation/types';
Expand Down Expand Up @@ -90,19 +90,24 @@ type SortedTransactions = {

const isSortableColumnName = (key: unknown): key is SortableColumnName => !!sortableColumnNames.find((val) => val === key);

const getTransactionValue = (transaction: OnyxTypes.Transaction, key: SortableColumnName) => {
if (key === CONST.SEARCH.TABLE_COLUMNS.DATE) {
const dateKey = transaction.modifiedCreated ? 'modifiedCreated' : 'created';
return transaction[dateKey];
const getTransactionValue = (transaction: OnyxTypes.Transaction, key: SortableColumnName, reportToSort: OnyxTypes.Report) => {
switch (key) {
case CONST.SEARCH.TABLE_COLUMNS.DATE:
return getCreated(transaction);
case CONST.SEARCH.TABLE_COLUMNS.MERCHANT:
return getMerchant(transaction);
case CONST.SEARCH.TABLE_COLUMNS.CATEGORY:
return getCategory(transaction);
case CONST.SEARCH.TABLE_COLUMNS.TAG:
return getTag(transaction);
case CONST.SEARCH.TABLE_COLUMNS.TOTAL_AMOUNT:
return getAmount(transaction, isExpenseReport(reportToSort), transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID);
case CONST.SEARCH.TABLE_COLUMNS.DESCRIPTION:
return Parser.htmlToText(transaction.comment?.comment ?? '');
default:
return transaction[key];
}

if (key === CONST.SEARCH.TABLE_COLUMNS.DESCRIPTION) {
return Parser.htmlToText(transaction.comment?.comment ?? '');
}

return transaction[key];
};

function MoneyRequestReportTransactionList({
report,
transactions,
Expand Down Expand Up @@ -170,12 +175,12 @@ function MoneyRequestReportTransactionList({

const sortedTransactions: TransactionWithOptionalHighlight[] = useMemo(() => {
return [...transactions]
.sort((a, b) => compareValues(getTransactionValue(a, sortBy), getTransactionValue(b, sortBy), sortOrder, sortBy, localeCompare))
.sort((a, b) => compareValues(getTransactionValue(a, sortBy, report), getTransactionValue(b, sortBy, report), sortOrder, sortBy, localeCompare, true))
.map((transaction) => ({
...transaction,
shouldBeHighlighted: newTransactions?.includes(transaction),
}));
}, [newTransactions, sortBy, sortOrder, transactions, localeCompare]);
}, [newTransactions, sortBy, sortOrder, transactions, localeCompare, report]);

const columnsToShow = useMemo(() => {
const columns = getColumnsToShow(session?.accountID, transactions, true);
Expand Down
8 changes: 4 additions & 4 deletions src/libs/SearchUIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1626,9 +1626,9 @@ function getSortedSections(

/**
* Compares two values based on a specified sorting order and column.
* Handles both string and numeric comparisons, with special handling for absolute values when sorting by total amount.
* Handles both string and numeric comparisons.
*/
function compareValues(a: unknown, b: unknown, sortOrder: SortOrder, sortBy: string, localeCompare: LocaleContextProps['localeCompare']): number {
function compareValues(a: unknown, b: unknown, sortOrder: SortOrder, sortBy: string, localeCompare: LocaleContextProps['localeCompare'], shouldCompareOriginalValue = false): number {
const isAsc = sortOrder === CONST.SEARCH.SORT_ORDER.ASC;

if (a === undefined || b === undefined) {
Expand All @@ -1640,8 +1640,8 @@ function compareValues(a: unknown, b: unknown, sortOrder: SortOrder, sortBy: str
}

if (typeof a === 'number' && typeof b === 'number') {
const aValue = sortBy === CONST.SEARCH.TABLE_COLUMNS.TOTAL_AMOUNT ? Math.abs(a) : a;
const bValue = sortBy === CONST.SEARCH.TABLE_COLUMNS.TOTAL_AMOUNT ? Math.abs(b) : b;
const aValue = sortBy === CONST.SEARCH.TABLE_COLUMNS.TOTAL_AMOUNT && !shouldCompareOriginalValue ? Math.abs(a) : a;
const bValue = sortBy === CONST.SEARCH.TABLE_COLUMNS.TOTAL_AMOUNT && !shouldCompareOriginalValue ? Math.abs(b) : b;
return isAsc ? aValue - bValue : bValue - aValue;
}

Expand Down
Loading