2013-09-29 29 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