1

I have one directory contain all the tif formate file ,near about 30 file with the name like that B_1 ,B_2...upto B_15 and F_1 ,F_2...upto F_1. when i am getting file from getfile method.

    Dim di As New IO.DirectoryInfo("c:\ABC\")
    Dim diar1 As IO.FileInfo() = di.GetFiles()

But while retriving using for each loop i am getting the result like B_1,B_10,B_11,B_12,B_13,B_14,B_15,B_2,B_3...upto B_9 same like F_1,F_10,F_11,F_12,F_13,F_14,F_15,F_2,F_3...upto F_9

But problem is that I want in pattern like B_1,B_2,B_3,B_4.....B_9,B_10,B_11......B_15 and then F_1,F_2,F_3,F_4.....F_9,F_10,F_11......F_15

Actually My task is getting all the file from directory and join all the tiff file file like F_1,B_1,F_2,B_2...F_9,B_9,F_10,B_10,F_11,B_11,....F_15,B_15

I have achieved everthing all means join tiff and but file start with B and F i am storing in respective arrayList but due to comming file in B_,B_10..so on thats why i am getting Problem...

Plz help me...

Dolphin
  • 138
  • 1
  • 3
  • 16

1 Answers1

1

The simplest solution is to create a method that returns a sort key as a string, for instance, in your situation, something like this should suffice:

Public Function GetFileInfoSortKey(fi As FileInfo) As String
    Dim parts() As String = fi.Name.Split("_"c)
    Dim sortKey As String = Nothing
    If parts.Length = 2 Then
        sortKey = parts(1).PadLeft(10) & parts(0)
    Else
        sortKey = fi.Name
    End If
    Return sortKey
End Function

Then, you can use that method to easily sort the array of FileInfo objects, like this:

Array.Sort(diar1, Function(x, y) GetFileInfoSortKey(x).CompareTo(GetFileInfoSortKey(y)))

If you don't care about keeping it as an array, you may want to use the OrderBy extension method provided by LINQ:

Dim diar1 As IEnumerable(Of FileInfo) = di.GetFiles().OrderBy(Of String)(AddressOf GetFileInfoSortKey)

Alternatively, if you are using an older version of Visual Studio which does not support lambda expressions, you can do it by creating a separate comparer method, like this:

Public Function FileInfoComparer(x As FileInfo, y As FileInfo) As Integer
    Return GetFileInfoSortKey(x).CompareTo(GetFileInfoSortKey(y))
End Function

Then you could call Array.Sort using that comparer method, like this:

Array.Sort(diar1, AddressOf FileInfoComparer)
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • Array.Sort method i am getting error below function "Expression expected" and also getting error below GetFileInfoSortKey function "end of statement expected" – Dolphin Mar 25 '14 at 04:21
  • 1
    You must be using an older version of Visual Studio which does not recognize the lambda expression. Is there some reason why you cannot upgrade to a more recent version? Even with the latest version of Visual Studio, you can still target your projects at the earliest versions of the .NET Framework. Lambda expressions, however, are supported by the later compiler, even when targeting the earlier frameworks. So, if you have a newer version of Visual studio, you can use lambda expressions even when targeting 2.0, for instance. – Steven Doggart Mar 25 '14 at 08:54
  • 1
    @Saurabh In case you cannot upgrade Visual Studio, I updated my answer to show how you can do the same thing without a lambda expression. – Steven Doggart Mar 25 '14 at 09:47