diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index c91d5f55d84c..38c3994b24b5 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -52,15 +52,19 @@ function getSortedReportActions(reportActions, shouldSortInDescendingOrder = fal } const invertedMultiplier = shouldSortInDescendingOrder ? -1 : 1; reportActions.sort((first, second) => { - // If only one action is a report created action, return the created action first. - if ((first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED || second.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) && first.actionName !== second.actionName) { - return ((first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) ? -1 : 1) * invertedMultiplier; - } - + // First sort by timestamp if (first.created !== second.created) { return (first.created < second.created ? -1 : 1) * invertedMultiplier; } + // Then by action type, ensuring that `CREATED` actions always come first if they have the same timestamp as another action type + if ((first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED || second.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) && first.actionName !== second.actionName) { + return ((first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) ? -1 : 1) * invertedMultiplier; + } + + // Then fallback on reportActionID as the final sorting criteria. It is a random number, + // but using this will ensure that the order of reportActions with the same created time and action type + // will be consistent across all users and devices return (first.reportActionID < second.reportActionID ? -1 : 1) * invertedMultiplier; }); return reportActions; diff --git a/tests/unit/ReportActionsUtilsTest.js b/tests/unit/ReportActionsUtilsTest.js index ccda75b52587..24fdcff9f354 100644 --- a/tests/unit/ReportActionsUtilsTest.js +++ b/tests/unit/ReportActionsUtilsTest.js @@ -10,46 +10,94 @@ describe('ReportActionsUtils', () => { { created: '2022-11-09 22:27:01.825', reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, }, { created: '2022-11-09 22:27:01.600', reportActionID: '6401435781022176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, }, // These reportActions were created in the same millisecond so should appear ordered by reportActionID { created: '2022-11-09 22:26:48.789', reportActionID: '2962390724708756', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, }, { created: '2022-11-09 22:26:48.789', reportActionID: '1609646094152486', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, }, { created: '2022-11-09 22:26:48.789', reportActionID: '1661970171066218', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, }, ], [ { created: '2022-11-09 22:26:48.789', reportActionID: '1609646094152486', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, }, { created: '2022-11-09 22:26:48.789', reportActionID: '1661970171066218', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, }, { created: '2022-11-09 22:26:48.789', reportActionID: '2962390724708756', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, }, { created: '2022-11-09 22:27:01.600', reportActionID: '6401435781022176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, }, { created: '2022-11-09 22:27:01.825', reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, + }, + ], + ], + [ + [ + // Given three reportActions with the same timestamp + { + created: '2023-01-10 22:25:47.132', + reportActionID: '3', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, + }, + { + created: '2023-01-10 22:25:47.132', + reportActionID: '2', + actionName: CONST.REPORT.ACTIONS.TYPE.CREATED, + }, + { + created: '2023-01-10 22:25:47.132', + reportActionID: '1', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, + }, + ], + [ + // The CREATED action should appear first, then we should sort by reportActionID + { + created: '2023-01-10 22:25:47.132', + reportActionID: '2', + actionName: CONST.REPORT.ACTIONS.TYPE.CREATED, + }, + { + created: '2023-01-10 22:25:47.132', + reportActionID: '1', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, + }, + { + created: '2023-01-10 22:25:47.132', + reportActionID: '3', + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, }, ], ],