如何按文件的最后修改日期排序?下面的代码只是按扩展名(json或jpg)对它们进行排序
function ambilData(req, res) {
let hasil;
ember
.getFiles(options)
.then(results => {
const files = results[0];
const tempArr = [];
let jsonArr = [],
jpgArr = [];
files.forEach(file => {
let nama = file.name,
mapped = nama.slice(9, nama.length);
tempArr.push(mapped)
});
tempArr.shift();
tempArr.forEach(file => {
if (file.split('.')[1] == 'json') {
jsonArr.push(file)
} else {
jpgArr.push(file)
}
})
res.send(listToMatrix(jsonArr, jpgArr))
})
.catch(err => {
console.error('ERROR:', err);
});
}
我正在使用Node.JS,并将使用express将上面的代码编写到REST API中。
答案 0 :(得分:0)
以下内容如何:
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Your Google Cloud Platform project ID
const projectId = 'YOUR PROJECT';
// Creates a client
const storage = new Storage({
projectId: projectId,
});
// The name for the new bucket
const bucketName = 'YOUR-BUCKET';
const bucket = storage.bucket(bucketName);
bucket.getFiles(null, (err,data) => {
data.sort((a, b) => {
if (a.metadata.updated > b.metadata.updated) {
return 1;
}
if (a.metadata.updated < b.metadata.updated) {
return -1;
}
return 0;
});
for (file of data) {
console.log(` ${file.metadata.name} - ${file.metadata.updated}`);
}
});
答案 1 :(得分:0)
检查响应中更新的元数据并使用
const data = [{file:"a",metadata:{updated:"2019-03-23T17:42:53.846"}},{file:"b",metadata:{updated:"2017-03-23T17:42:53.846"}},{file:"c",metadata:{updated:"2016-03-23T17:42:53.846"}}];
//sorts ascending
let sortAsc = data.sort((a,b) => new Date(a.metadata.updated) - new Date(b.metadata.updated));
//sorts descending
let sortDesc = data.sort((a,b) => new Date(b.metadata.updated) - new Date(a.metadata.updated));