2013-09-29 70 views
0

的DirectoryInfo至极的列表中包含的文件夹的名字,像这些:Bubble使用LINQ排序列表?

80's 
90's 
2000 
2001 

的问题是,“IO.Directory.GetDirectories”函数返回通用的微软排序,所以我的清单被排序为:

2000 
2001 
80's 
90's 

我知道算法泡泡排序(总是我看到的使用FOR和GEN我不喜欢任何Bubble排序方法),我希望如果使用LINQ或其他改进的方法可以简化Bubble Sort,但不希望在内存中创建额外的对象。

我怎样才能冒泡排序列表(中的DirectoryInfo)通过Directory.Name财产? (显然我想保留DirectoryInfo对象,而不是返回一对已排序的字符串),也可以对其进行冒泡排序而不用使用LINQ扩展重新分配列表?

UPDATE:

如果有人需要的信息,这是我用得到的DirectoryInfo列表功能:

' Get Folders 
Private Function Get_Folders(ByVal directory As String, ByVal recursive As Boolean) As List(Of IO.DirectoryInfo) 
    Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly) 
    Return IO.Directory.GetDirectories(directory, "*", searchOpt).Select(Function(p) New IO.DirectoryInfo(p)).ToList 
End Function 

更新2

按照关于问题评论的建议,我试图简化一个使用正则表达式和LINQ扩展将文件夹名称整理为整数来排序它们,问题在于它失败了,因为我有一些文件夹无法转换为数字,这是一个示例文件夹名称:

80's 
90's 
2000-2006 
2007 
2008 
Classic 
B.S.O 
Maquetas 

我的问题是如果我能exlude的非数字字符的文件夹排序时,然后该排除的文件夹追加到排序的“整数”文件夹名称,请问这只是为了不要让所有的文件夹两次生成两个不同的列表来加入它们。

另外请注意文件夹名称“2000-2006”,如果我将名称转换为整数,排序时我不会得到预期的结果。

所以我怎么可能泡泡排序列表文件夹名称内容对待他们是什么?,字符串,而不是数字。

Public Class Form1 

Dim regex As New System.Text.RegularExpressions.Regex("\D") 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown 

    For Each folder In Get_Folders("E:\Música\Canciones", False) _ 
         .OrderBy(Function(x) Convert.ToInt32(regex.Replace(x.Name, ""))) 

     MsgBox(folder.Name) 
     ' Exception here, because a folder named "B.S.O" and other named as "Classic", 
     ' obviouslly they can't be converted to Integer :(

    Next 

End Sub 

' Get Folders 
Private Function Get_Folders(ByVal directory As String, ByVal recursive As Boolean) As List(Of IO.DirectoryInfo) 
    Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly) 
    Return IO.Directory.GetDirectories(directory, "*", searchOpt).Select(Function(p) New IO.DirectoryInfo(p)).ToList 
End Function 

End Class 
+0

http://stackoverflow.com/a/11052176/932418 –

+0

@L。B Thankyou,该解决方案有点硬编码我试图删除所有不必要的东西,如分裂和子字符串,但也许我可以需要帮助来润饰该解决方案,因为我不知道结果是否将与我的列表一起工作。 – ElektroStudios

+0

Elektro Hacker,我测试了这个解决方案,它适用于你的情况(我说的是我的回答不是被指责的:))。 –

回答

1

我翻译的代码in referenced question使用Telerik的online converter。它也适用于你的情况。

Public Shared Function CustomSort(list As IEnumerable(Of String)) As IEnumerable(Of String) 
    Dim maxLen As Integer = list.[Select](Function(s) s.Length).Max() 

    Return list.[Select](Function(s) New With { _ 
     Key .OrgStr = s, _ 
     Key .SortStr = System.Text.RegularExpressions.Regex.Replace(s, "(\d+)|(\D+)", Function(m) m.Value.PadLeft(maxLen, If(Char.IsDigit(m.Value(0)), " "c, Char.MaxValue))) _ 
    }).OrderBy(Function(x) x.SortStr).[Select](Function(x) x.OrgStr) 
End Function 
+0

我试着用它作为参数传递一个io.directoryinfo列表,在函数内做一些修改,但对我来说不可能,你能帮我编辑代码吗?编辑:哦,最后我得到它加入“.Tolist”时返回的数据,非常感谢,我会发布修改 – ElektroStudios

+0

它的工作真的很棒。 – ElektroStudios

1

的@ L.B解决方案稍加修改,我希望这可以帮助别人:

Public Shared Function CustomSort(list As List(Of IO.DirectoryInfo)) As List(Of IO.DirectoryInfo) 

     Dim maxLen As Integer = list.[Select](Function(s) s.Name.Length).Max() 

     Return list.[Select](Function(s) New With { _ 
      Key .OrgStr = s, _ 
      Key .SortStr = System.Text.RegularExpressions.Regex.Replace(s.Name, "(\d+)|(\D+)", Function(m) m.Value.PadLeft(maxLen, If(Char.IsDigit(m.Value(0)), " "c, Char.MaxValue))) _ 
     }).OrderBy(Function(x) x.SortStr).[Select](Function(x) x.OrgStr).ToList 
    End Function