VSCode 文件夹未按字母顺序排序 [英] VSCode Folders Not Sorting Alphabetically

查看:35
本文介绍了VSCode 文件夹未按字母顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 代码资源管理器窗格奇怪地对我的文件夹进行排序.我想在将其报告为错误之前验证这确实是一个问题.我有 vscode 1.47.2.我很确定这并不总是一个问题.举个例子:

Visual Studio Code Explorer pane is sorting my folders oddly. I want to validate this is truly an issue before reporting it as a bug. I have vscode 1.47.2. I'm fairly sure this wasn't always an issue. Here's an example:

我希望有一个名为aaa.xxx.iii"的文件夹.将按字母顺序排列在名为aaa.yyy"的文件夹之前.事实上,当我在文件资源管理器中查看列表时,确实排序正确.

I would expect that a folder named "aaa.xxx.iii" would be alphabetically sorted before the folder named "aaa.yyy". In fact, when I look at the list in File Explorer, it is indeed sorted correctly.

我没有使用工作区文件.我搜索了整个目录结构,但没有以 .code-workspace 为后缀的文件.我知道这可能是多根工作区中的一个问题.但是,我只是使用打开文件夹"打开此解决方案.

I am not using a workspace file. I have searched the entire directory structure and I have no files suffixed with .code-workspace. I know this can be an issue in multi-root workspaces. However, I am just using "Open Folder" to open this solution.

我还检查了工作区设置、功能、资源管理器、排序下的设置,并将其设置为默认值(字母顺序、文件夹在文件之前).我尝试更改为修改后的排序顺序,但没有成功.

I have also checked the settings under Workspace Settings, Features, Explorer, Sort and it is set to default (Alphabetic, Folders before Files). I tried changing to Modified sort order and back with no luck.

推荐答案

我希望有一个名为aaa.xxx.iii"的文件夹.将按字母顺序排列在名为aaa.yyy"的文件夹之前

I would expect that a folder named "aaa.xxx.iii" would be alphabetically sorted before the folder named "aaa.yyy"

按字母顺序排序,只是ASCII排序!

It is alphabetically sorted, just ASCII sorted!

ASCII 值:

a = 97
b = 98
i = 105
x = 120
y = 121
. = 46

因此:

aaa         = 291
aaa.xxx     = 697
aaa.yyy     = 700
aaa.xxx.iii = 1,012

然而,他们的逻辑似乎有些不同.他们实际上通过文件名正则表达式拆分文件名(并且他们使用相同的逻辑来比较目录和文件名).他们首先有效地与文件名进行比较,甚至在使用以下正则表达式考虑扩展名之前:

However, there seems to be some variation to their logic. They actually split filenames via a filename regex (and they use the same logic for comparing directories and filenames). They effectively compare against a filename first, before even considering the extension using the following regex:

const FileNameMatch = /^(.*?)(\.([^.]*))?$/;

然后它只会在返回0时才考虑扩展,不大于或小于.

And then it will only consider extensions when it returns 0, for not greater than or less than.

使用该正则表达式:在您的示例中,aaa.xxxaaa 是文件名,.xxx 是扩展名".

Using that regex: in your example, aaa.xxx, aaa is the filename, .xxx is the 'extension`.

使用aaa.yyy;aaa 是文件名,与 aaa.xxx.iii 比较,你知道 .iii 是扩展名.因此,aaa.yyy 或不带 .yyy 扩展名的名称:aaa <aaa.xxxaaa.xxx.iii 带有扩展名

With aaa.yyy; aaa is the filename and compared against aaa.xxx.iii, you get it .iii is the extension. Ergo, aaa.yyy, or the name without the .yyy extension: aaa < aaa.xxx or aaa.xxx.iii with the extension

这是他们的逻辑:

explorerViewer.ts

:

export function compareFileNamesNumeric(one: string | null, other: string | null): number {
    const [oneName, oneExtension] = extractNameAndExtension(one, true);
    const [otherName, otherExtension] = extractNameAndExtension(other, true);
    const collatorNumeric = intlFileNameCollatorNumeric.value.collator;
    const collatorNumericCaseInsensitive = intlFileNameCollatorNumericCaseInsenstive.value.collator;
    let result;

    // Check for name differences, comparing numbers numerically instead of alphabetically.
    result = compareAndDisambiguateByLength(collatorNumeric, oneName, otherName);
    if (result !== 0) {
        return result;
    }

    // Check for case insensitive extension differences, comparing numbers numerically instead of alphabetically.
    result = compareAndDisambiguateByLength(collatorNumericCaseInsensitive, oneExtension, otherExtension);
    if (result !== 0) {
        return result;
    }

    // Disambiguate the extension case if needed.
    if (oneExtension !== otherExtension) {
        return collatorNumeric.compare(oneExtension, otherExtension);
    }

    return 0;
}

所以看起来,在大多数情况下,他们只是使用 Intl.Collat​​or 带有基本名称值 a 逻辑,当然有一些变化.

So it appears, for the most part, they are just using Intl.Collator with basic name value a < b logic, with some variations of course.

它们似乎也通过长度消除歧义,这意味着 foo1foo01 被认为是相等的.a

It also appears they disambiguate by length, meaning foo1 and foo01 are considered equal. line 169

您描述的设置仅影响它们的显示方式",排序方式是肯定的,但主要是它们向用户显示的方式,而不是它们在我解释时以编程方式排序的方式.

The setting you described only affects 'how they are displayed', in a way sorted yes, but mostly how they are shown to the user, not really how they are sorted programmatically as I interpret it.

//控制资源管理器中文件和文件夹的排序顺序.

// Controls sorting order of files and folders in the explorer.

//- 默认:文件和文件夹按名称按字母顺序排序.文件夹显示在文件之前.

// - default: Files and folders are sorted by their names, in alphabetical order. Folders are displayed before files.

//- 混合:文件和文件夹按名称按字母顺序排序.文件与文件夹交织在一起.

// - mixed: Files and folders are sorted by their names, in alphabetical order. Files are interwoven with folders.

//- filesFirst:文件和文件夹按名称按字母顺序排序.文件显示在文件夹之前.

// - filesFirst: Files and folders are sorted by their names, in alphabetical order. Files are displayed before folders.

//- 类型:文件和文件夹按其扩展名按字母顺序排序.文件夹显示在文件之前.

// - type: Files and folders are sorted by their extensions, in alphabetical order. Folders are displayed before files.

//- 修改:文件和文件夹按最后修改日期降序排列.文件夹显示在文件之前.

// - modified: Files and folders are sorted by last modified date, in descending order. Folders are displayed before files.

explorer.sortOrder":默认",

"explorer.sortOrder": "default",

所以它更像是文件/目录的展示设置,而不是名称本身.如果您查看 explorerViewer.ts 控制流,您将看到:

So it's more of a presentation setting for files/directories not the names themselves. If you look at the explorerViewer.ts control flow you will see:

  switch (sortOrder) {
    case 'type':
    if (statA.isDirectory && !statB.isDirectory) {
        return -1;
    }

    if (statB.isDirectory && !statA.isDirectory) {
        return 1;
    }

    if (statA.isDirectory && statB.isDirectory) {
        return compareFileNamesNumeric(statA.name, statB.name);
    }

    break;

现在我们可以想象回到目录如何使用相同的功能进行排序;重新 if (statA.isDirectory && statB.isDirectory)

And now we can visualize going back to how directories are sorted with the same function; re if (statA.isDirectory && statB.isDirectory)

这篇关于VSCode 文件夹未按字母顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆