排序像Windows资源管理器(数字和按字母顺序)文件夹名的数组 - VB.NET [英] Sorting an array of folder names like Windows Explorer (Numerically and Alphabetically) - VB.NET

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

问题描述

我杀死自己和脱水试图让这个数组排序。

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

我有一个包含所产生的目录的数组;

I have an array containing directories generated by;

昏暗的文件夹()作为字符串= Directory.GetDirectories(ROOTPATH​​)

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

我需要他们进行排序,使他们看起来像Windows资源管理器在Win7 / Vista中。 - 数字和字母顺序按文件夹名称

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(文件夹)的结果。

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

我用Google搜索,并发现一类需要使用IComparable接口的元素进行排序写入。作为一个supernewbie ......我真的不知道它是如何可以做到的。大多数例子我看着有多维数组和按键:•...

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 ...

如果排序可以适用于文件名(代替foldernames)或含有两个文件夹和文件的阵列的阵列...在这种情况下,排序的文件夹显示在顶部和排序的文件下面会更好.. ,这甚至可能吗?

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?

任何帮助将大大appriciated ...: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资源管理器。该函数将字符串作为数字数字部分,从而使1排序前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 1 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天全站免登陆