-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorter.js
76 lines (64 loc) · 2.25 KB
/
sorter.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Create a new temporary list called “activeList”.
// You begin on the left of your axisList, adding the first item to the activeList.
// Now you have a look at the next item in the axisList and compare it with all items
// currently in the activeList (at the moment just one):
// - If the new item’s left is greater then the current activeList-item right, then remove
// the activeList-item from the activeList
// - otherwise report a possible collision between the new axisList-item and the current
// activeList-item.
// Add the new item itself to the activeList and continue with the next item in the axisList.
let operations = 0;
function removeAtIndex(list, index) {
// console.log('removeAtIndex', [...list.slice(0,index),...list.slice(index + 1)])
return [...list.slice(0, index), ...list.slice(index + 1)];
}
function sortSweep(sorted, rowNum) {
const activeList = [];
let clash = [];
let list = [...sorted];
let result = [];
// 1. You begin on the left of your axisList, adding the first item to the activeList.
activeList.push({
startTime: new Date(list[0].start).getTime(),
endTime: new Date(list[0].end).getTime(),
...list[0],
});
for (let i = 1; i < list.length; i++) {
const current = {
startTime: new Date(list[i].start).getTime(),
endTime: new Date(list[i].end).getTime(),
...list[i],
};
let addToActiveList;
for (let j = 0; j < activeList.length; j++) {
operations++;
// 2. If the new item’s left is greater then the current activeList-item right, then remove
// the activeList-item from the activeList
if (current.startTime > activeList[j]["endTime"]) {
activeList.splice(j, 1);
j--;
result.push(current);
} else {
if (
current.startTime < activeList[j]["endTime"] &&
activeList[j]["startTime"] < current.endTime
) {
clash.push(current);
list = removeAtIndex(list, i);
addToActiveList = current;
i--;
break;
}
}
}
if (addToActiveList !== current) {
activeList.push(current);
}
}
let test = [];
if (clash.length) {
test = sortSweep(clash, rowNum + 1);
}
return [list, ...test];
}
module.exports = sortSweep;