Results 1 to 14 of 14

Thread: [02/03] Problem Sorting 2 D Array

Hybrid View

  1. #1
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [02/03] Problem Sorting 2 D Array

    Try this now... I didn't test it (because I don't have the text file), but it should work though.
    VB Code:
    1. Private Function Foo(ByVal strInputFilePath As String, Optional ByVal strOutputFilePath As String = "") As Boolean
    2.         Try
    3.             'Open the file & read all the contents
    4.             Dim reader As IO.StreamReader = IO.File.OpenText(strInputFilePath)
    5.             Dim strContent As String = reader.ReadToEnd
    6.             reader.Close()
    7.  
    8.             'Split the file contents by line to an array then sort it
    9.             Dim seperators As Char() = {Chr(10), Chr(13)}
    10.             Dim temps() As String = strContent.Split(seperators)
    11.  
    12.             Dim strBldr As New System.Text.StringBuilder
    13.             Dim i As Integer = 0
    14.             For i = 0 To temps.GetUpperBound(0)
    15.                 If temps(i).Trim().Length >= 23 Then
    16.                     strBldr.Append(temps(i).Substring(18, 5) & temps(i) & ChrW(13))
    17.                 End If
    18.             Next
    19.  
    20.             Dim lines() As String = strBldr.ToString.Split(seperators)
    21.             Array.Sort(lines)
    22.             'Write back to the file
    23.             Dim outputPath As String = strInputFilePath
    24.             If strOutputFilePath <> "" Then
    25.                 outputPath = strOutputFilePath
    26.             End If
    27.             Dim writer As IO.StreamWriter = IO.File.CreateText(outputPath)
    28.             For i = 0 To lines.GetUpperBound(0)
    29.                 writer.WriteLine(lines(i).Substring(5))
    30.             Next
    31.             writer.Close()
    32.         Catch ex As Exception
    33.             MsgBox(ex.Message)
    34.             Return False
    35.         End Try
    36.         Return True
    37.     End Function
    Last edited by stanav; Jan 25th, 2007 at 04:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width