[英]Bubble Sort a List using LINQ?
我有一個DirectoryInfo列表,其中包含以下文件夾名稱:
80's
90's
2000
2001
問題是“ IO.Directory.GetDirectories ”函數返回通用Microsoft排序,所以我的列表排序如下:
2000
2001
80's
90's
我知道冒泡排序的算法(總是我看到FOR的用法和生成其他時間對象,我不喜歡我見過的任何冒泡排序方法)我希望如果使用LINQ或者簡化冒泡排序其他改進的方法但不是a既不在內存中創建額外的對象。
我如何通過其Directory.Name屬性冒泡排序 列表(DirectoryInfo) ? (顯然我想保留DirectoryInfo對象,而不是返回幾個已排序的字符串),也可能在不使用LINQ擴展重新分配列表的情況下對其進行冒泡排序?
更新:
如果有人需要這些信息,那么這就是我用來獲取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
我的問題是,如果我可以在排序時排除非Digits文件夾,然后將排除的文件夾附加到已排序的“整數”文件夾名稱,我問這只是為了不兩次獲取所有文件夾以生成兩個不同的列表來加入他們。
另請注意文件夾名稱“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
我使用Telerik的在線轉換器 在引用的問題中翻譯了代碼。 它也適用於您的情況。
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
對@LB解決方案的一點修改,我希望這有助於其他人:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.