在node.js中使用具有足够精度(纳秒)的Date实例对文件进行排序

时间:2016-07-28 01:13:06

标签: node.js date precision

我想获得Date instance的纳秒精度(如果需要)来对我用这个bash文件创建的十个数字进行排序

#!/bin/bash
mkdir -p count
touch count/1
touch count/2
touch count/3
touch count/4
touch count/5
touch count/6
touch count/7
touch count/8
touch count/9
touch count/10

我有以下脚本,我想按如下方式排序:

[1,2,3,4,5,6,7,8,9,10]

但我明白了:

[ '/home/kaartje2go/node/delete-all-but-x-files/count',
  '/home/kaartje2go/node/delete-all-but-x-files/count/9',
  '/home/kaartje2go/node/delete-all-but-x-files/count/10',
  '/home/kaartje2go/node/delete-all-but-x-files/count/2',
  '/home/kaartje2go/node/delete-all-but-x-files/count/3',
  '/home/kaartje2go/node/delete-all-but-x-files/count/5',
  '/home/kaartje2go/node/delete-all-but-x-files/count/6',
  '/home/kaartje2go/node/delete-all-but-x-files/count/7',
  '/home/kaartje2go/node/delete-all-but-x-files/count/8',
  '/home/kaartje2go/node/delete-all-but-x-files/count/4',
  '/home/kaartje2go/node/delete-all-but-x-files/count/1' ]

我使用了以下脚本:

'use strict'

const fs = require('fs-extra')
const items = [] // files, directories, symlinks, etc
fs.walk('./count')
  .on('data', function (item) {
    items.push(item)
  })
  .on('end', function () {
    const mtimes =  items.sort((a,b) => b.stats.mtime - a.stats.mtime);
    console.log(mtimes.map(mtime => mtime.path));
 });

当我通过nautilus(Ubuntu)进行排序时,它可以正常工作:

enter image description here

我认为这应该是原生的(仅限JS),但如果错误请纠正我。

0 个答案:

没有答案