当前位置: 首页 > 编程语言 > C#

文件夹名数组排序,如Windows资源管理器(数字和字母)-VB.NET分享

时间:2023-04-10 15:46:37 C#

文件夹名数组排序,如Windows资源管理器(数字和字母)-VB.NET我是自杀和脱水试图让这个数组排序。我有一个包含生成目录的数组;DimFolders()AsString=Directory.GetDirectories(RootPath)我需要对它们进行排序,以便它们显示在win7/vista的Windows资源管理器中。–文件夹名称的数字和字母顺序。文件夹名称包含字母和数字,有时仅包含字母或仅包含数字。简单的Array.Sort(文件夹)结果C:inetpubwwwrootrootpath1C:inetpubwwwrootrootpath10C:inetpubwwwrootrootpath100C:inetpubwwwrootrootpath1004C:inetpubwwwrootrootpath101C:inetpubwwwrootrootpath11C:inetpubwwwrootrootpath12C:inetpubwwwrootrootpath2C:inetpubwwwrootrootpath3C:inetpubwwwrootrootpath4C:inetpubwwwrootrootpath5C:inetpubwwwrootrootpath6C:inetpubwwwrootrootpath7C:inetpubwwwrootrootpath8C:inetpubwwwrootrootpath87skjndC:inetpubwwwrootrootpath89sdf93kmw3C:inetpubwwwrootrootpath9C:inetpubwwwrootrootpathadC:inetpubwwwrootrootpathbinC:inetpubwwwrootrootpathdarkC:inetpubwwwrootrootpatherkC:inetpubwwwrootrootpathjkh23978yoaslkd3C:inetpubwwwrootrootpathlk2309asC:inetpubwwwrootrootpathworkC:inetpubwwwrootrootpathzone我想拥有的(以及Windows资源管理器显示的内容)是……C:inetpubwwwrootrootpath1C:inetpubwwwrootrootpath2C:inetpubwwwrootrootpath3C:inetpubwwwrootrootpath4C:inetpubwwwrootrootpath5C:inetpubwwwrootrootpath6C:inetpubwwwrootrootpath7C:inetpubwwwrootrootpath8C:inetpubwwwrootrootpath9C:inetpubwwwrootrootpath10C:inetpubwwwrootrootpath11C:inetpubwwwrootrootpath12C:inetpubwwwrootrootpath87skjndC:inetpubwwwrootrootpath89sdf93kmw3C:inetpubwwwrootrootpath100C:inetpubwwwrootrootpath101C:inetpubwwwrootrootpath1004C:inetpubwwwrootrootpathadC:inetpubwwwrootrootpathbinC:inetpubwwwrootrootpathdarkC:inetpubwwwrootrootpatherkC:inetpubwwwrootrootpathjkh23978yoaslkd3C:inetpubwwwrootrootpathlk2309asC:inetpubwwwrootrootpathworkC:inetpubwwwrootrootpathzone我在谷歌上搜索了一下,发现作为一个超级新手,我需要编写一个使用IComparable对元素进行排序的类……我真的不知道该怎么做。我见过的大多数示例都有多维数组和键:S...如果排序可以应用于文件名数组(而不是文件夹名称)或包含文件夹和文件的数组会更好......在这个case接下来,排好序的文件夹出现在最上面,排好序的文件出现在下面……这有可能吗?任何帮助将不胜感激......:谢谢。您需要实现IComparer,而不是创建一个实现IComparable的类。不同之处在于IComparer具有比较两个对象所必需的“知识”,而IComparable是由知道如何将自身与其他对象进行比较的类实现的。Windows资源管理器对文件名进行排序的方式是使用名为StrCmpLogicalW的函数。您可以在自己的IComparer中使用此函数来获得与Windows资源管理器相同的排序行为。此函数将字符串的数字部分视为数字,以便9在10之前排序。x,字符串y);publicintCompare(stringx,stringy){returnStrCmpLogicalW(x,y);}}Array.Sort(unsortedNames,newMyComparer());因为我刚刚注意到这个问题被标记为VB...请原谅我生锈的VB!公共类MyComparer实现IComparer(OfString)DeclareUnicodeFunctionStrCmpLogicalWLib"shlwapi.dll"(_ByVals1AsString,_ByVals2AsString)AsInt32PublicFunctionCompare(ByvalxasString,ByvalyasString)AsInteger_实现System.Collections.Generic.IComparer(OfString).CompareReturnStrCmpLogicalW(x,y)EndFunctionEndClassArray.Sort也有一个IComparer参数,如果你不喜欢默认的,你可以重写排序行为.具体做法见Array.Sortmethod(T[],IComparer)对大家有用,需要多了解C#学习教程。希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: