From 13a17b8e28a190d10ccfd2df47c4c9dfa095d1ff Mon Sep 17 00:00:00 2001 From: Vera Kahn Date: Thu, 12 Sep 2024 14:53:57 -0400 Subject: [PATCH] update sort method --- lib/util.js | 15 ++++++++++++++- test/util.test.js | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 2cdbe1ba..0fe77078 100644 --- a/lib/util.js +++ b/lib/util.js @@ -2,7 +2,20 @@ const logger = require('./logger') const isItemNyplOwned = require('./ownership_determination').isItemNyplOwned exports.sortOnPropWithUndefinedLast = (property) => { - return (a, b) => a[property]?.[0] || Number.MAX_SAFE_INTEGER > b[property]?.[0] || Number.MAX_SAFE_INTEGER ? -1 : 1 + return function (a, b) { + // equal items sort equally + if (a[property]?.[0] === b[property]?.[0]) { + return 0 + } + // nulls sort after anything else + if (!a[property]?.[0]) { + return 1 + } + if (!b[property]?.[0]) { + return -1 + } + return a[property]?.[0] > b[property]?.[0] ? -1 : 1 + } } exports.buildJsonLdContext = function (prefixes) { diff --git a/test/util.test.js b/test/util.test.js index 8625ce0a..39a1a138 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -4,7 +4,7 @@ const mangledEnumerationChronologyItems = require('./fixtures/mangled_enumeratio const util = require('../lib/util') describe('Util', function () { - describe.only('sortOnPropWithUndefinedLast', () => { + describe('sortOnPropWithUndefinedLast', () => { it('sorts undefined last', () => { const sortedItemEnums = mangledEnumerationChronologyItems .sort(util.sortOnPropWithUndefinedLast('enumerationChronology_sort'))