通过Javascript获取有序的文件列表

时间:2016-01-12 12:15:32

标签: javascript node.js

我有一个文件夹,其中包含文件:

s1, s2, s3, ... s9, s10, s11, ... s99, s100, s101, s102, s103, ...,

和脚本,它打印这些文件的列表:

filesystem = require('fs');
filesystem.readdirSync('./folder').forEach(function (file) {
    console.log(file);
});

但是我得到一个这样的无序列表:

s0, s1, s10, s100, s101, s102, s103, ... s11, s110, s111, s112, s12

我知道一个原因,但我希望获得“数字”有序列表,如下所示:

s1, s2, s3, ... s9, s10, s11, ... s99, s100, s101, s102, s103, ...,

5 个答案:

答案 0 :(得分:3)

您可以使用Array.prototype.sort通过剥离s并将其余部分解析为数字来对数组进行排序:

filesystem = require('fs');
var result = filesystem.readdirSync('./folder').sort(function (a, b) {
    return (+a.substr(1)) - (+b.substr(1));
});

答案 1 :(得分:1)

filesystem.readdir('./folder', function(err, files) {
    files.sort(function(file1, file2) {
        var file1Int =  parseInt(file1.replace( /^\D+/g, '')),
            file2Int =  parseInt(file2.replace( /^\D+/g, ''));

        if (file1Int < file2Int) {
            return -1;
        }
        if (file1Int > file2Int) {
           return 1;
        }
        return 0;

    }).forEach(function(file) {
        console.log(file);
    });
});

答案 2 :(得分:0)

您可以这样做:

$arr = Array ( [0] => 9314802498 [1] => 9314801890 [2] => MOSWAPELE ESTHER [3] => BENNETH );

$arr[1] = 'MOSWAPELE ESTHER';
$arr[2] = '9314801890';

答案 3 :(得分:0)

var fs = require('fs'),
  files = fs.readdirSync('.'),
  sorted = files.sort(function (a, b) {
    return Number(a.substr(1)) > Number(b.substr(1));
  });
console.log(sorted);
// eg. [ 's0', 's1', 's10', 's100', 's101' ]

答案 4 :(得分:0)

我觉得虽然其中一些答案现在可以使用,但它们都不适用于一般情况,所以我想我会尝试一种更通用的方法,这可能对其他人来说很有用。读这个问题。

function getSort(caseInsensitive) {
    caseInsensitive = !!caseInsensitive;
    // Splits a string in to string and number fragments, parsing integers along the way.
    function getFragments(string) {
        var strings = string.split(/\d/);
        var numbers = string.split(/\D/);
        if (caseInsensitive === true) {
            strings = strings.map(function(string) {
                return string.toLowerCase();
            });
        }
        // Remove any empty strings (there's likely to be one at the start or at the end).
        var fragments = strings.filter(function(value) {
            return value.length > 0;
        });
        var insertIndex = 0;
        // Insert numbers in the correct place in the fragments array.
        for (var i = 0; i < numbers.length; i++) {
            if (numbers[i].length > 0) {
                fragments.splice(insertIndex, 0, parseInt(numbers[i]));
                // Add one to insert index to account for the element we just added.
                insertIndex++;
            }
            insertIndex++;
        }
        return fragments;
    }

    // Actual comparison function.
    return function(lhs, rhs) {
        var lhsFragments = getFragments(lhs);
        var rhsFragments = getFragments(rhs);

        for (var i = 0; i < lhsFragments.length; i++) {
            // Sort right-hand-side in front of left-hand-side if left-hand-side has more fragments.
            if (i >= rhsFragments.length) {
                return 1;
            }
            if (lhsFragments[i] !== rhsFragments[i]) {
                if (lhsFragments[i] < rhsFragments[i]) {
                    return -1;
                } else {
                    return 1;
                }
            }
        }
        // Sort left-hand-side in front of right-hand-side if right-hand-side has more fragments.
        if (lhsFragments.length < rhsFragments.length) {
            return -1;
        }
        return 0;
    }
}

var caseSensitiveSort = getSort();
var caseInsensitiveSort = getSort(true);
var testArray = [
    'x1',
    'X',
    'r22s1',
    'r2s2',
    's2',
    'r1t1',
    'r2',
    's1t1',
    's1',
    's1t2',
    't',
    's'
];
console.log(testArray.sort(caseSensitiveSort));
console.log(testArray.sort(caseInsensitiveSort));

输出:

["X", "r1t1", "r2", "r2s2", "r22s1", "s", "s1", "s1t1", "s1t2", "s2", "t", "x1"]
["r1t1", "r2", "r2s2", "r22s1", "s", "s1", "s1t1", "s1t2", "s2", "t", "X", "x1"]

当然,它涉及的有点多,但并不过分。它还应该处理比之前发布的答案更多的情况,特别是它不会忽略比较中的实际字符串内容。希望它对某人有用。