forked from computiq/GIZ-pass-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.js
79 lines (65 loc) · 2.6 KB
/
task.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
77
78
79
// ES6
// import { readFileSync, writeFileSync } from 'fs';
let fs = require('fs');
class SortType{
// Using static keyword so we can use the class without initiating an object
static ASC = 'ASC';
static DESC = 'DESC';
}
class ArraySorter{
numbers = []
constructor(numbers){
this.numbers = numbers;
// freeze the object so numbers be immutable
Object.freeze(this);
}
/**
* Bubble Sort The array
* - param:: <sortType> Either SortType.ASC or SortType.DESC
* - return:: The Sorted array
*/
bubbleSortList = (sortType) => {
let numbersToSort = this.numbers;
for (let i = 0; i < numbersToSort.length; i++) {
for (let j = 0; j < numbersToSort.length - 1; j++) {
if (sortType == SortType.ASC && numbersToSort[j] > numbersToSort[j + 1]) {
numbersToSort = this._switchItems(j, numbersToSort)
}
else if(sortType == SortType.DESC && numbersToSort[j] < numbersToSort[j + 1]) {
numbersToSort = this._switchItems(j, numbersToSort)
}
}
}
return numbersToSort
}
/**
* Switch array items
* - param:: <index> Either SortType.ASC or SortType.DESC
* - param:: <numbersToSort> Either SortType.ASC or SortType.DESC
* - return:: The numbers array to contine sorting it
*/
_switchItems = (index, numbersToSort) => {
let temp = numbersToSort[index];
numbersToSort[index] = numbersToSort[index + 1]
numbersToSort[index + 1] = temp;
return numbersToSort;
}
}
const Numbers = [5, 8, 0, 1, 9, 11, 15, 16];
console.log("Original numbers list: ", Numbers)
const sorter = new ArraySorter(Numbers);
console.log("Numbers list After ASC sorting: ", sorter.bubbleSortList(SortType.ASC))
console.log("Numbers list After DESC sorting: ", sorter.bubbleSortList(SortType.DESC))
// Reading from data.txt file
let fileName = "data.txt"
let data = fs.readFileSync(fileName, 'utf8');
// Convert the string that has been read from the file to an array
let fileNumbers = JSON.parse("[" + data + "]");
// Another way to convert the string to an array
// let x = data.split(",").map(Number);
console.log("Original numbers list from " + fileName + " file: ", fileNumbers)
const sorter2 = new ArraySorter(fileNumbers);
let fileNumbersSortedASC = sorter2.bubbleSortList(SortType.ASC)
console.log("Numbers list from " + fileName + " file After ASC sorting: ", fileNumbersSortedASC)
// Write the sorted array to output.txt file
fs.writeFileSync("output.txt", `${fileNumbersSortedASC}`)