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'))