对一系列文件夹名称进行排序,例如 Windows 资源管理器(按数字和字母顺序)- VB.NET [英] Sorting an array of folder names like Windows Explorer (Numerically and Alphabetically) - VB.NET

查看:85
本文介绍了对一系列文件夹名称进行排序,例如 Windows 资源管理器(按数字和字母顺序)- VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自杀并试图让这个数组排序.

I'm killing myself and dehydrating trying to get this array to sort.

我有一个包含由生成的目录的数组;

I have an array containing directories generated by;

Dim Folders() As String = Directory.GetDirectories(RootPath)

Dim Folders() As String = Directory.GetDirectories(RootPath)

我需要对它们进行排序,使它们看起来像在 win7/vista 中的 Windows 资源管理器中.-- 按文件夹名称的数字和字母顺序.

I need them to be sorted so they appear like in windows explorer in win7 / vista. -- numerically and alphabetically by folder names.

文件夹名称包含字母和数字,有时仅包含字母或仅数字.

The folder names contain both letters and numbers, sometimes letters only or numbers only.

简单的 Array.Sort(Folders) 结果

The simple Array.Sort(Folders) results in

C:\inetpub\wwwroot\rootpath\1
C:\inetpub\wwwroot\rootpath\10
C:\inetpub\wwwroot\rootpath\100
C:\inetpub\wwwroot\rootpath\1004
C:\inetpub\wwwroot\rootpath\101
C:\inetpub\wwwroot\rootpath\11
C:\inetpub\wwwroot\rootpath\12
C:\inetpub\wwwroot\rootpath\2
C:\inetpub\wwwroot\rootpath\3
C:\inetpub\wwwroot\rootpath\4
C:\inetpub\wwwroot\rootpath\5
C:\inetpub\wwwroot\rootpath\6
C:\inetpub\wwwroot\rootpath\7
C:\inetpub\wwwroot\rootpath\8
C:\inetpub\wwwroot\rootpath\87skjnd
C:\inetpub\wwwroot\rootpath\89sdf93kmw3
C:\inetpub\wwwroot\rootpath\9
C:\inetpub\wwwroot\rootpath\ad
C:\inetpub\wwwroot\rootpath\bin
C:\inetpub\wwwroot\rootpath\dark
C:\inetpub\wwwroot\rootpath\erk
C:\inetpub\wwwroot\rootpath\jkh23978yoaslkd3
C:\inetpub\wwwroot\rootpath\lk2309as
C:\inetpub\wwwroot\rootpath\work
C:\inetpub\wwwroot\rootpath\zone

我想要的(以及 Windows 资源管理器显示的)是......

What I want to have (and what windows explorer displays) is ...

C:\inetpub\wwwroot\rootpath\1
C:\inetpub\wwwroot\rootpath\2
C:\inetpub\wwwroot\rootpath\3
C:\inetpub\wwwroot\rootpath\4
C:\inetpub\wwwroot\rootpath\5
C:\inetpub\wwwroot\rootpath\6
C:\inetpub\wwwroot\rootpath\7
C:\inetpub\wwwroot\rootpath\8
C:\inetpub\wwwroot\rootpath\9
C:\inetpub\wwwroot\rootpath\10
C:\inetpub\wwwroot\rootpath\11
C:\inetpub\wwwroot\rootpath\12
C:\inetpub\wwwroot\rootpath\87skjnd
C:\inetpub\wwwroot\rootpath\89sdf93kmw3
C:\inetpub\wwwroot\rootpath\100
C:\inetpub\wwwroot\rootpath\101
C:\inetpub\wwwroot\rootpath\1004
C:\inetpub\wwwroot\rootpath\ad
C:\inetpub\wwwroot\rootpath\bin
C:\inetpub\wwwroot\rootpath\dark
C:\inetpub\wwwroot\rootpath\erk
C:\inetpub\wwwroot\rootpath\jkh23978yoaslkd3
C:\inetpub\wwwroot\rootpath\lk2309as
C:\inetpub\wwwroot\rootpath\work
C:\inetpub\wwwroot\rootpath\zone

我用谷歌搜索,发现需要编写一个使用 IComparable 对元素进行排序的类.作为一个超级新手......我真的不知道如何做到这一点.我看过的大多数例子都有多维数组和键:S ...

I googled and found out that a class needs to be written that uses IComparable to sort the elements. Being a supernewbie ... I really don't know how it can be done. Most examples I looked at had multi dimensional arrays and keys :S ...

如果排序可以应用于文件名数组(而不是文件夹名)或包含文件夹和文件的数组,那就更好了……在这种情况下,排序的文件夹出现在顶部,排序的文件在下面... 这可能吗?

would be even nicer if the sorting could be applied to an array of filenames (instead of foldernames) or an array containing both folders and files ... in which case the sorted folders appear at the top and sorted files below ... is this even possible?

任何帮助都将得到极大的帮助... :D 谢谢.

Any help will be greatly appriciated ... :D thanks.

推荐答案

您需要实现一个 IComparer,而不是创建一个实现 IComparable 的类.不同之处在于 IComparer 具有比较两个对象所必需的知识",而 IComparable 由知道如何将自身与其他对象进行比较的类实现.

You would need to implement an IComparer, as opposed to creating a class that implements IComparable. The difference is that an IComparer has the necessary "knowledge" to compare two objects whereas IComparable is implemented by a class that knows how to compare itself against something else.

Windows 资源管理器对文件名进行排序的方式是使用名为 StrCmpLogicalW.您可以在自己的 IComparer 中使用此函数来获得与 Windows 资源管理器相同的排序行为.此函数将字符串的数字部分视为数字,以便 9 在 10 之前排序.

And the way Windows Explorer sorts filenames is using a function called StrCmpLogicalW. You can use this function in your own IComparer to get the same sort behavior as Windows Explorer. This function treats numeric parts of strings as numbers so that 9 sorts before 10.

public class MyComparer : IComparer<string> {

    [DllImport("shlwapi.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
    static extern int StrCmpLogicalW(String x, String y);

    public int Compare(string x, string y) {
        return StrCmpLogicalW(x, y);
    }

}

Array.Sort(unsortedNames, new MyComparer());

而且因为我刚刚注意到这个问题被标记为 VB...请原谅我生疏的 VB!

And since I just noticed the question is tagged VB... Forgive my rusty VB!

Public Class MyComparer
    Implements IComparer(Of String)

    Declare Unicode Function StrCmpLogicalW Lib "shlwapi.dll" ( _
        ByVal s1 As String, _
        ByVal s2 As String) As Int32

    Public Function Compare(Byval x as String, Byval y as String) As Integer _
        Implements System.Collections.Generic.IComparer(Of String).Compare

        Return StrCmpLogicalW(x, y)

    End Function

End Class

这篇关于对一系列文件夹名称进行排序,例如 Windows 资源管理器(按数字和字母顺序)- VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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