Skip to content

Commit 4bcf3e6

Browse files
author
Gérard Collin
committed
fix: insertinsortedlist
1 parent d33de1c commit 4bcf3e6

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

libs/xt-store/projects/store/src/store-provider/xt-store-provider-helper.spec.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {XtStoreProviderHelper} from './xt-store-provider-helper';
22
import { XtStoreGroupByAggregate, XtStoreGroupBy } from '../xt-reporting';
3-
import { XtGroupByOperation, XtSortByDirection } from '../xt-store-parameters';
3+
import { XtGroupByOperation, XtSortBy, XtSortByDirection } from '../xt-store-parameters';
44
import { ManagedData, ManagedDataHandler, SpecialFields, xtTypeManager } from 'xt-type';
55
import { describe, expect, it } from 'vitest';
66

@@ -311,5 +311,48 @@ describe('Store Provider Helper', () => {
311311
expect(byNameSort.map((val)=> val.name+val.value)).toEqual(['cTest4','cTest10', 'aTest1']);
312312

313313
})
314+
315+
it ('should support sorted insertion correctly', () => {
316+
const testArray=[{
317+
name: 'cTest',
318+
value: 4,
319+
date: new Date (1970,10,5)
320+
},{
321+
name:'cTest',
322+
value: 10,
323+
date: new Date(2010,8,4)
324+
},{
325+
name:'aTest',
326+
value:1,
327+
date: new Date(1960, 5,10)
328+
},];
329+
330+
const sortOptions=[{
331+
by:'name',
332+
direction:XtSortByDirection.Descending
333+
}, {
334+
by:'value',
335+
direction:XtSortByDirection.Ascending
336+
}] as XtSortBy<any>[];
337+
338+
const byNameInsert = XtStoreProviderHelper.insertInSortedList({
339+
name:'bTest',
340+
value:7,
341+
date:new Date()
342+
},
343+
[...testArray], sortOptions);
344+
345+
expect(byNameInsert.map((val)=> val.name+val.value)).toEqual(['cTest4','cTest10','bTest7', 'aTest1']);
346+
347+
const byValueInsert = XtStoreProviderHelper.insertInSortedList({
348+
name:'cTest',
349+
value:6,
350+
date:new Date()
351+
},
352+
[...testArray], sortOptions);
353+
354+
expect(byValueInsert.map((val)=> val.name+val.value)).toEqual(['cTest4','cTest6','cTest10', 'aTest1']);
355+
356+
})
314357
}
315358
);

libs/xt-store/projects/store/src/store-provider/xt-store-provider-helper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ export class XtStoreProviderHelper {
147147
} else {
148148
const compareFunction=this.compareFunction(sortOptions);
149149
for (let index=0;index<list.length;index++) {
150-
if (list[index]>toInsert) {
151-
return list.splice(index, 0, toInsert);
150+
if (compareFunction(list[index],toInsert)>0) {
151+
list.splice(index, 0, toInsert);
152+
return list;
152153
}
153154
}
154155
list.push(toInsert);

0 commit comments

Comments
 (0)