-
Notifications
You must be signed in to change notification settings - Fork 0
/
prioritysort.js
70 lines (63 loc) · 1.76 KB
/
prioritysort.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
/**
* The purpose of this algorith is to sort
* an array by prioritzing based on the target given
* amongst lists of values in the array
* E.g [2, 3, 5, 3,2,3,2] to make to a sort while making
* 2 as the target parameter we will get
* [2,2,2,3,5,3,3]
*/
const getTargetList = (arr, target) => {
const targetList = [];
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
if (element == target) {
targetList.push(target);
}
}
return targetList;
};
const getNonTargetList = (arr, target) => {
const nonTargetList = [];
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
if (element != target) {
nonTargetList.push(target);
}
}
return nonTargetList;
};
const prioritySort = (arr, target) => {
const nonTargetList = [];
const targetList = [];
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
if (element == target) {
targetList.push(target);
} else {
nonTargetList.push(element);
}
}
return [...targetList, ...nonTargetList];
};
// console.log(prioritySort([2,3,5,3,2,3,2], 2)) => [2,2,2,3,5,3,3]
const prioritySort2 = (arr, targets) => {
for (let index = 0; index < targets.length; index++) {
const element = targets[targets.length - index - 1];
arr = prioritySort(arr, element);
}
return arr;
};
const getTheSorted = (arr, target) => {
let rr = getTargetList(arr, target);
return rr;
};
const getTheSorteds = (arr, targets) => {
let m = [];
for (let index = 0; index < targets.length; index++) {
const element = targets[index];
m.push(...getTheSorted(arr, element));
}
return m;
};
console.log(getTheSorteds([2, 3, 5, 3, 2, 3, 2, 4, 6], [2, 6]));
//export default prioritySort;