-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
textdb-utils.js
43 lines (37 loc) · 993 Bytes
/
textdb-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
exports.sort = function(builder, item) {
var length = builder.response.length;
if (length <= builder.$take2) {
length = builder.response.push(item);
if (length >= builder.$take2) {
builder.response.sort(builder.$sort);
builder.$sorted = true;
}
return true;
} else
return chunkysort(builder, item);
};
exports.sortfinal = function(builder) {
builder.response.sort(builder.$sort);
};
function chunkysort(builder, item) {
var beg = 0;
var length = builder.response.length;
var tmp = length - 1;
var sort = builder.$sort(item, builder.response[tmp]);
if (sort !== -1)
return;
tmp = length / 2 >> 0;
sort = builder.$sort(item, builder.response[tmp]);
if (sort !== -1)
beg = tmp + 1;
for (var i = beg; i < length; i++) {
var old = builder.response[i];
var sort = builder.$sort(item, old);
if (sort !== 1) {
for (var j = length - 1; j > i; j--)
builder.response[j] = builder.response[j - 1];
builder.response[i] = item;
return true;
}
}
}