fix(collection)!: default sort comparison algorithm (#10412)

BREAKING CHANGE: This replaces the previously inaccurate default sort algorithm, which may alter sort results where a user-defined comparison function is not provided.
This commit is contained in:
René
2024-12-02 08:23:48 +00:00
committed by GitHub
parent f5445c8104
commit 5f0d28c0fe
2 changed files with 50 additions and 10 deletions

View File

@@ -829,6 +829,33 @@ describe('sort() tests', () => {
const coll = createCollectionFrom(['a', 5], ['b', 3], ['c', 1]);
expect(coll.sort()).toStrictEqual(createCollectionFrom(['c', 1], ['b', 3], ['a', 5]));
});
describe('returns correct sort order for test values', () => {
const defaultSort = Collection['defaultSort']; // eslint-disable-line @typescript-eslint/dot-notation
const testDefaultSortOrder = (firstValue: any, secondValue: any, result: number) => {
expect(defaultSort(firstValue, secondValue)).toStrictEqual(result);
expect(defaultSort(secondValue, firstValue)).toStrictEqual(result ? result * -1 : 0);
};
test('correctly evaluates sort order of undefined', () => {
testDefaultSortOrder(undefined, undefined, 0);
testDefaultSortOrder(0, undefined, -1);
});
test('correctly evaluates numeric values stringwise', () => {
testDefaultSortOrder(-1, -2, -1); // "-1" before "-2"
testDefaultSortOrder(1, '1', 0); // "1" equal to "1"
testDefaultSortOrder(1, '1.0', -1); // "1" before "1.0"
testDefaultSortOrder(1.1, '1.1', 0); // "1.1" equal to "1.1"
testDefaultSortOrder('01', 1, -1); // "01" before "1"
testDefaultSortOrder(1, 1n, 0); // "1" equal to "1"
testDefaultSortOrder(Number.NaN, 'NaN', 0); // "NaN" equal to "NaN"
});
test('evaluates object literals as equal', () => {
testDefaultSortOrder({ a: 1 }, { b: 2 }, 0);
});
});
});
});