|
■No63235 (SAQ さん) に返信
正規表現を使ったほうが楽でしょう。
使わなくても独自に文字列解析をすれば可能です。
以下正規表現を使ったサンプルです。
Imports System.Text.RegularExpressions
Public Class Form1
Private Class CompSorter
Implements IComparer(Of String)
Private FilePat As Regex
Public Sub New()
FilePat = New Regex("(?<Alp>.+?)-(?<Num>\d+)_Ver(?<Ver>\d+)\.txt")
End Sub
Public Function Compare(x As String, y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
Dim FileX As New IO.FileInfo(x)
Dim FileY As New IO.FileInfo(y)
Dim xm = FilePat.Match(FileX.Name)
Dim ym = FilePat.Match(FileY.Name)
Compare = 0
If xm.Success AndAlso ym.Success Then
Dim AlpX = xm.Groups("Alp").Value
Dim AlpY = ym.Groups("Alp").Value
Dim NumX = CInt(xm.Groups("Num").Value)
Dim NumY = CInt(ym.Groups("Num").Value)
Dim VerX = CInt(xm.Groups("Ver").Value)
Dim VerY = CInt(ym.Groups("Ver").Value)
Dim ret = NumX.CompareTo(NumY)
If ret = 0 Then
ret = VerX.CompareTo(VerY)
End If
If ret = 0 Then
ret = String.Compare(AlpX, AlpY)
End If
Return ret
ElseIf xm.Success Then
Return 1
ElseIf ym.Success Then
Return -1
End If
End Function
End Class
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim FileNames() As String = New String() { _
"C:\Folder_01\AA-001_Ver2.txt",
"C:\Folder_02\CC-002_Ver1.txt",
"C:\Folder_03\CC-003_Ver3.txt",
"C:\Folder_04\BB-002_Ver1.txt",
"C:\Folder_05\AA-001_Ver1.txt"
}
Dim Sorter As New CompSorter
Array.Sort(FileNames, Sorter)
End Sub
End Class
|