-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortByHeight.js
36 lines (31 loc) · 987 Bytes
/
SortByHeight.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
// first version, avoding the usage of "native functions" but using a not so clear if clause sorting the input array in place
function sortByHeight(a) {
let temp = [];
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a.length; j++){
if (a[i] < a[j] && a[i] != -1){
temp = a[j]
a[j] = a[i]
a[i] = temp
}
}
} return a;
}
// creating a new array with removed -1 (= trees), sorting the new array and inserting those values in the original array
function sortByHeight2(a) {
let array2 = a;
array2 = array2.filter((element) => {
if(element != -1) {
return element;
}
}).sort((a,b) => {
return a-b;
});
for (let i = 0; i < a.length; i++) {
if (a[i] != -1) {
a[i] = array2.shift();
}
} return a;
}
let people = [-1, 150, 190, 170, -1, -1, 160, 180]
console.log(sortByHeight2(people));