Skip to content

Commit 065cf2c

Browse files
committed
Adds default ordering for PagingPredicate if no comparator is given by user
1 parent 5369d85 commit 065cf2c

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/Util.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Long = require('long');
22
import {PagingPredicate} from './serialization/DefaultPredicates';
33
import {IterationType} from './core/Predicate';
44
import * as assert from 'assert';
5+
import {Comparator} from './core/Comparator';
56
export function assertNotNull(v: any) {
67
assert.notEqual(v, null, 'Non null value expected.');
78
}
@@ -48,9 +49,10 @@ export function getSortedQueryResultSet(list: Array<any>, predicate: PagingPredi
4849
return list;
4950
}
5051
var comparatorObject = predicate.getComparator();
51-
if (comparatorObject != null) {
52-
list.sort(comparatorObject.sort.bind(comparatorObject));
52+
if (comparatorObject == null) {
53+
comparatorObject = createComparator(predicate.getIterationType());
5354
}
55+
list.sort(comparatorObject.sort.bind(comparatorObject));
5456
var nearestAnchorEntry = (predicate == null) ? null : predicate.getNearestAnchorEntry();
5557
var nearestPage = nearestAnchorEntry[0];
5658
var page = predicate.getPage();
@@ -76,6 +78,23 @@ export function getSortedQueryResultSet(list: Array<any>, predicate: PagingPredi
7678
});
7779
}
7880

81+
function createComparator(iterationType: IterationType): Comparator {
82+
var object: Comparator = {
83+
sort: function(a: [any, any], b: [any, any]): number {
84+
return 0;
85+
}
86+
};
87+
switch (iterationType) {
88+
case IterationType.KEY:
89+
object.sort = (e1: [any, any], e2: [any, any]) => {return e1[0] < e2[0] ? -1 : +(e1[0] > e2[0]); }; break;
90+
case IterationType.ENTRY:
91+
object.sort = (e1: [any, any], e2: [any, any]) => {return e1[1] < e2[1] ? -1 : +(e1[1] > e2[1]); }; break;
92+
case IterationType.VALUE:
93+
object.sort = (e1: [any, any], e2: [any, any]) => {return e1[1] < e2[1] ? -1 : +(e1[1] > e2[1]); }; break;
94+
}
95+
return object;
96+
}
97+
7998
function setAnchor(list: Array<any>, predicate: PagingPredicate, nearestPage: number) {
8099
assert(list.length > 0);
81100
var size = list.length;

0 commit comments

Comments
 (0)