-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSorter.js
66 lines (52 loc) · 1.34 KB
/
FileSorter.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
var fs = require('fs');
var directory = process.argv[2];
var fileList = [];
var count = 0;
function FileSorter(path){
if (!(this instanceof FileSorter)){
return new FileSorter(path);
}
this.path = path;
}
FileSorter.prototype.sort = function(){
var self = this;
fs.readdir(this.path, function readDirHandler(err, list){
if (err) return console.error(err);
// console.log(list);
count = list.length;
list.forEach(self.filestat);
});
}
FileSorter.prototype.sortList = function(a, b){
// sortList = function(a, b){
if(a.mtime > b.mtime){
console.log('hello');
return -1;
}
else if (a.mtime < b.mtime){
return 1;
}
console.log('sorting help');
return 0;
}
FileSorter.prototype.filestat = function(file, index, array){
var self = this;
fs.stat(file, function(err, stat){
var outerSelf = self;
count--;
console.log(count);
if (err) return console.error(err);
// console.log(stat.mtime);
fileList.push({"file" : file, "mtime" : stat.mtime });
console.log(stat.mtime.getTime());
if (count === 0){
console.log('sorting', fileList[0].mtime.getTime(), fileList[0],file);
// fileList.sort(sortList);
// console.log(outerSelf);
fileList.sort(self.sortList);
console.log(fileList);
}
});
}
var fileSorter = FileSorter('./');
fileSorter.sort();