To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Jan 25th, 2007, 09:40 AM   #1
pricejt
New Member
 
Join Date: Jan 07
Posts: 9
pricejt is an unknown quantity at this point (<10)
[02/03] Problem Sorting 2 D Array

I have a log file that I am trying to parse out and sort. Eventually i will print it out into xml. I have already pulled the file out of the log and read it into memory, i have also parsed the log file into a two dimensional array to be able to sort it. The reason i have to parse and put into a two d array is because of this:

07-01-15 00:00:00 00116: < RCPT TO:<email@eamil.com>
07-01-15 00:00:00 00120: Connection opened by pool-55-555-55-55. <blah>
07-01-15 00:00:00 00116: > 551 user does not exist

If you see in the file the third set of numbers is out of order.
00116
00120
00116

So what i have done is put those values into one side of the array and put the rest of the file into the other side. I now want to sort on column 0 and then write it back out to the file.

This is what the array would look like:
00116 | 07-01-15 00:00:00 00116: < RCPT TO:<email@eamil.com>
00120 | 07-01-15 00:00:00 00120: Connection opened by pool-55-555-55-5
00116 | 07-01-15 00:00:00 00116: > 551 user does not exist

The result would look like:
00116 | 07-01-15 00:00:00 00116: < RCPT TO:<email@eamil.com>
00116 | 07-01-15 00:00:00 00116: > 551 user does not exist
00120 | 07-01-15 00:00:00 00120: Connection opened by pool-55-555-55-5

The Code:
This function Reads the file in and splits it into the two d array:

VB Code:
  1. Private Function OrderFile(ByVal fileLocation As String)
  2.         Dim fs As System.IO.FileStream
  3.         Dim sr As StreamReader
  4.         Try
  5.             'We have our variables, lets attempt to open it 
  6.             fs = New System.IO.FileStream(fileLocation, _
  7.                 FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
  8.         Catch
  9.             'Make sure that it exists, and if it doesn't display an error 
  10.             MessageBox.Show("Count not open the file.  Make sure '" & fileLocation & _
  11.                 "' exists in the location shown.")
  12.         Finally
  13.             'It works, but we are going to try one last method 
  14.             If fs.CanRead = True Then
  15.                 'This is where we would read from the file 
  16.                 txtFileDisplay.Text = "File opened!"
  17.                 'We are done with the file now, so close it 
  18.                 fs.Close()
  19.             End If
  20.         End Try
  21.         ''Create the StreamReader and associate the filestream with it 
  22.         Dim myReader As New System.IO.StreamReader(fileLocation)
  23.         ''Read the entire text, and set it to a string 
  24.         Dim sFileContents As String = myReader.ReadToEnd()
  25.         ''Print it to the textbox 
  26.         txtFileDisplay.Text = sFileContents
  27.         ''Close everything when you are finished 
  28.         myReader.Close()
  29.         ' Split file data into array of lines
  30.             Dim textReader As New System.IO.StreamReader(fileLocation)
  31.             Dim WholeLine As String
  32.         Dim lines(,) As String
  33.         Dim r As Integer
  34.         Dim Count = 0 '
  35.         WholeLine = textReader.ReadLine()
  36.         While Not WholeLine.Length <= 0
  37.             ReDim Preserve lines(1, Count)
  38.             lines(0, Count) = WholeLine.Substring(18, 5)
  39.             r = WholeLine.Length
  40.             lines(1, Count) = WholeLine.Substring(0, r)
  41.             Count = Count + 1
  42.             WholeLine = textReader.ReadLine()
  43.         End While
  44.         Call QuickSort(lines, 0, 0, 0)
  45.     End Function

This is the Quick Sort to sort the two d array:

VB Code:
  1. Sub QuickSort(ByVal vec, ByVal loBound, ByVal hiBound, ByVal SortField)
  2.         '==--------------------------------------------------------==
  3.         '== Sort a 2 dimensional array on SortField                ==
  4.         '==                                                        ==
  5.         '== This procedure is adapted from the algorithm given in: ==
  6.         '==    ~ Data Abstractions & Structures using C++ by ~     ==
  7.         '==    ~ Mark Headington and David Riley, pg. 586    ~     ==
  8.         '== Quicksort is the fastest array sorting routine for     ==
  9.         '== unordered arrays.  Its big O is  n log n               ==
  10.         '==                                                        ==
  11.         '== Parameters:                                            ==
  12.         '== vec       - array to be sorted                         ==
  13.         '== SortField - The field to sort on (2nd dimension value) ==
  14.         '== loBound and hiBound are simply the upper and lower     ==
  15.         '==   bounds of the array's 1st dimension.  It's probably  ==
  16.         '==   easiest to use the LBound and UBound functions to    ==
  17.         '==   set these.                                           ==
  18.         '==--------------------------------------------------------==
  19.         Dim pivot(), loSwap, hiSwap, temp, counter
  20.         ReDim pivot(UBound(vec, 2))
  21.         loSwap = LBound(vec)
  22.         hiSwap = UBound(vec, 2)
  23.         '== Two items to sort
  24.         If hiBound - loBound = 1 Then
  25.             If vec(loBound, SortField) > vec(hiBound, SortField) Then Call SwapRows(vec, hiBound, loBound)
  26.         End If
  27.         '== Three or more items to sort
  28.         For counter = 0 To UBound(vec, 2)
  29.             pivot(counter) = vec(Int((loBound + hiBound) / 2), counter)
  30.             vec(Int((loBound + hiBound) / 2), counter) = vec(loBound, counter)
  31.             vec(loBound, counter) = pivot(counter)
  32.         Next
  33.         loSwap = loBound + 1
  34.         hiSwap = hiBound
  35.         Do
  36.             '== Find the right loSwap
  37.             While loSwap < hiSwap And vec(loSwap, Int(SortField)) <= pivot(SortField)
  38.                 loSwap = loSwap + 1
  39.             End While
  40.             '== Find the right hiSwap
  41.             While vec(hiSwap, SortField) > pivot(Int(SortField))
  42.                 hiSwap = hiSwap - 1
  43.             End While
  44.             '== Swap values if loSwap is less then hiSwap
  45.             If loSwap < hiSwap Then Call SwapRows(vec, loSwap, hiSwap)
  46.         Loop While loSwap < hiSwap
  47.         For counter = 0 To UBound(vec, 2)
  48.             vec(loBound, counter) = vec(hiSwap, counter)
  49.             vec(hiSwap, counter) = pivot(counter)
  50.         Next
  51.         '== Recursively call function .. the beauty of Quicksort
  52.         '== 2 or more items in first section
  53.         If loBound < (hiSwap - 1) Then Call QuickSort(vec, loBound, hiSwap - 1, Int(SortField))
  54.         '== 2 or more items in second section
  55.         If hiSwap + 1 < hiBound Then Call QuickSort(vec, hiSwap + 1, hiBound, Int(SortField))
  56.         Call PrintArray(pivot, 0, 0)
  57.     End Sub  'QuickSort
  58.  

Swap Rows Used by QuickSort:
VB Code:
  1. Sub SwapRows(ByVal ary, ByVal row1, ByVal row2)
  2.         '== This proc swaps two rows of an array
  3.         Dim x, tempvar
  4.         For x = 0 To UBound(ary, 2)
  5.             tempvar = ary(row1, x)
  6.             ary(row1, x) = ary(row2, x)
  7.             ary(row2, x) = tempvar
  8.         Next
  9.     End Sub  'SwapRows
  10.  

Print New Array. This is where i am getting an error it looks like the array getting passed to this function isn't created correctly or filling it.

VB Code:
  1. Sub PrintArray(ByVal vec, ByVal lo, ByVal hi)
  2.         '==-----------------------------------------==
  3.         '== Print out an array from the lo bound    ==
  4.         '==  to the hi bound.                       ==
  5.         '==                                         ==
  6.         '==-----------------------------------------==
  7.         Dim sw As StreamWriter = File.CreateText("input.txt")
  8.         Dim i, j
  9.         lo = LBound(vec)
  10.         hi = UBound(vec)
  11.         For i = lo To hi
  12.             For j = 0 To UBound(vec)
  13.                 sw.Write(vec(i, j))
  14.             Next
  15.         Next
  16.         sw.Close()
  17.     End Sub  'PrintArray
  18.  
pricejt is offline   Reply With Quote
Old Jan 25th, 2007, 12:01 PM   #2
axion_sa
Frenzied Member
 
axion_sa's Avatar
 
Join Date: Jan 02
Location: Joburg, RSA
Posts: 1,705
axion_sa is a jewel in the rough (200+)axion_sa is a jewel in the rough (200+)axion_sa is a jewel in the rough (200+)
Re: [02/03] Problem Sorting 2 D Array

You could save yourself a lot of code...

First off - I don't have VB installed, so the code sample will be in C#.
The code makes use of the System.Collections.IComparer interface to compare two values - in this case, the string arrays. To do the actual sorting, the Array.Sort method has an overload that accepts an IComparer.

The comparer:
PHP Code:
    /// <summary>
    /// Can be used by the Array.Sort method to sort string array values by a specified index.
    /// </summary>
    
public class StringArrayComparer: IComparer
    
{
        
private int _compareIndex;

        
/// <summary>
        /// Gets or sets the array index that will be used to sort.
        /// </summary>
        
public int CompareIndex
        
{
            
get { return _compareIndex; }
            
set { _compareIndex = value; }
        }

        
/// <summary>
        /// Compares the values at the <see cref="CompareIndex"/> index.
        /// </summary>
        /// <remarks>Assumes the <paramref name="x" /> and <paramref name="y" /> parameters are string arrays.</remarks>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        
public int Compare(object x, object y)
        {
            
string[] xValues = (string[]) x;
            
string[] yValues = (string[]) y;
            
            return (
xValues[this.CompareIndex].CompareTo(yValues[this.CompareIndex]));
        }
    }
The test code:
PHP Code:
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    
class Class1
    
{
        
/// <summary>
        /// The main entry point for the application.
        /// </summary>
        
[STAThread]
        static
void Main(string[] args)
        {
            
string[][] values = new string[][]
                {
                    new
string[] {"0105", "Pom"},
                    new
string[] {"0015", "Foo"},
                    new
string[] {"0016", "Abc"},
                    new
string[] {"0012", "Lkj"},
                    new
string[] {"0001", "Tyu"}
                };
            
            
StringArrayComparer comparer = new StringArrayComparer();
            
comparer.CompareIndex = 0;
            
            Array.
Sort(values, comparer);
            
            for (
int i = 0; i < values.Length; i++)
            {
                
Console.Write("Row [{0}]: ", i.ToString());
                for (
int j = 0; j < values[i].Length; j++)
                    
Console.Write("{0}\t", values[i][j]);
                
Console.Write("\n");
            }
            
            
Console.WriteLine("\nDone!");
            
Console.ReadLine();
        }
    }
axion_sa is offline   Reply With Quote
Old Jan 25th, 2007, 12:35 PM   #3
stimbo
Frenzied Member
 
stimbo's Avatar
 
Join Date: Jun 06
Location: UK
Posts: 1,744
stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)
Re: [02/03] Problem Sorting 2 D Array

Maybe this isn't very accurate or even something you want to explore but why don't you put the numbers at the beginning of the string?? Load them into a list and use the Sort method that comes with it? You may want to check this out. I don't know if it would always be correct but maybe you could adapt it to help.

VB Code:
  1. Dim myString As New List(Of String)
  2.         myString.Add("02323: 07-01-16 00:00:00: < RCPT TO:<email@eamil.com>")
  3.         myString.Add("00116: 07-01-15 00:00:00: < RCPT TO:<email@eamil.com>")
  4.         myString.Add("00120: 07-01-15 00:00:00: Connection opened by pool-55-555-55-55. <blah>")
  5.         myString.Add("00116: 07-01-15 00:00:00: > 551 user does not exist")
  6.         myString.Sort()
  7.         ListBox1.Items.AddRange(myString.ToArray)
__________________
Stim

Free VB.NET Book Chapter
Visual Basic 2005 Cookbook Sample Chapter
stimbo is offline   Reply With Quote
Old Jan 25th, 2007, 12:50 PM   #4
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 6,854
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: [02/03] Problem Sorting 2 D Array

With the text format you have in the text file and what you want to do, I think just a 1-d string array will get the job done.
Try this:
VB Code:
  1. 'Open the file & read all the contents
  2. Dim reader As IO.StreamReader = IO.File.OpenText("path here")
  3.         Dim strContent As String = reader.ReadToEnd
  4.         reader.Close()
  5.         'Split the file contents by line to an array then sort it
  6.         Dim lines() As String = strContent.Split(ChrW(13))
  7.         Array.Sort(lines)
  8.         'Write back to the file
  9.         Dim writer As IO.StreamWriter = IO.File.CreateText("path here")
  10.         For i As Integer = 0 To lines.GetUpperBound(0)
  11.             writer.WriteLine(lines(i))
  12.         Next
  13.         writer.Close()
stanav is online now   Reply With Quote
Old Jan 25th, 2007, 12:56 PM   #5
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 6,854
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: [02/03] Problem Sorting 2 D Array

Quote:
Originally Posted by stimbo
Maybe this isn't very accurate or even something you want to explore but why don't you put the numbers at the beginning of the string?? Load them into a list and use the Sort method that comes with it? You may want to check this out. I don't know if it would always be correct but maybe you could adapt it to help.

VB Code:
  1. Dim myString As New List(Of String)
  2.         myString.Add("02323: 07-01-16 00:00:00: < RCPT TO:<email@eamil.com>")
  3.         myString.Add("00116: 07-01-15 00:00:00: < RCPT TO:<email@eamil.com>")
  4.         myString.Add("00120: 07-01-15 00:00:00: Connection opened by pool-55-555-55-55. <blah>")
  5.         myString.Add("00116: 07-01-15 00:00:00: > 551 user does not exist")
  6.         myString.Sort()
  7.         ListBox1.Items.AddRange(myString.ToArray)
To Stimbo:
Oopssss.... He's using [02/03] so List isn't an option...

To Pricejt:
Regarding Stimbo code, you would use an Arraylist instead of List in [02/03]
stanav is online now   Reply With Quote
Old Jan 25th, 2007, 01:21 PM   #6
pricejt
New Member
 
Join Date: Jan 07
Posts: 9
pricejt is an unknown quantity at this point (<10)
Re: [02/03] Problem Sorting 2 D Array

Thank you guys for all your feedback. axion_sa I am not very good with C# so dont know how easy that will be for me.

Stanav I tried implimenting your code but it didn't sort it. I am thinking that its not spliting on the right part of the file.

stimbo: I dont know about your method The log file i have probably has somewhere around 64,000 lines that i would have to loop through and add. But i don tkonw maybe that is the best way.
pricejt is offline   Reply With Quote
Old Jan 25th, 2007, 02:43 PM   #7
stimbo
Frenzied Member
 
stimbo's Avatar
 
Join Date: Jun 06
Location: UK
Posts: 1,744
stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)
Re: [02/03] Problem Sorting 2 D Array

Ooopps indeed, I didn't even see the 02/03.

If there is that many lines then ICompare would definitely be better.

There's a really nice example of it (ICompare) somewhere on this forum. Do a search for it and you will probably come across it. I think one of the moderators posted it. You should be able to adapt it from that.
__________________
Stim

Free VB.NET Book Chapter
Visual Basic 2005 Cookbook Sample Chapter
stimbo is offline   Reply With Quote
Old Jan 25th, 2007, 02:47 PM   #8
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 6,854
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: [02/03] Problem Sorting 2 D Array

Quote:
Originally Posted by pricejt
Stanav I tried implimenting your code but it didn't sort it. I am thinking that its not spliting on the right part of the file.
That could be because the lines in your text file are terminated with a new line character instead of a cartridge return character.
Try replacing this line
VB Code:
  1. Dim lines() As String = strContent.Split(ChrW(13))
With these lines and see if it works
VB Code:
  1. Dim seperators As Char() = {ChrW(10), ChrW(13)}
  2. Dim lines() As String = strContent.Split(seperators)
stanav is online now   Reply With Quote
Old Jan 25th, 2007, 02:56 PM   #9
pricejt
New Member
 
Join Date: Jan 07
Posts: 9
pricejt is an unknown quantity at this point (<10)
Re: [02/03] Problem Sorting 2 D Array

Stanav: you are right that worked. However with the 64,000 lines it is very slow. Do you think that the iCompare would be a lot faster than this way?
pricejt is offline   Reply With Quote
Old Jan 25th, 2007, 02:57 PM   #10
axion_sa
Frenzied Member
 
axion_sa's Avatar
 
Join Date: Jan 02
Location: Joburg, RSA
Posts: 1,705
axion_sa is a jewel in the rough (200+)axion_sa is a jewel in the rough (200+)axion_sa is a jewel in the rough (200+)
Re: [02/03] Problem Sorting 2 D Array

**sigh

http://www.kamalpatel.net/ConvertCSharp2VB.aspx

VB Code:
  1. Public Class StringArrayComparer
  2.      Implements IComparer
  3.         Private _compareIndex As Integer
  4.  
  5.         Public Property CompareIndex() As Integer
  6.             Get
  7.                  Return _compareIndex
  8.             End Get
  9.             Set (ByVal Value As Integer)
  10.                  _compareIndex = value
  11.             End Set
  12.         End Property
  13.  
  14.         Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
  15.             Dim xValues() As String = CType(x, String())
  16.             Dim yValues() As String = CType(y, String())
  17.  
  18.             Return (xValues(Me.CompareIndex).CompareTo(yValues(Me.CompareIndex)))
  19.         End Function
  20.     End Class
  21. '----------------------------------------------------------------
  22. ' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
  23. ' Developed by: Kamal Patel ([url]http://www.KamalPatel.net[/url])
  24. '----------------------------------------------------------------
  25.  

VB Code:
  1. 'Error: Converting Properties
  2.     Class Class1
  3.         <STAThread> _
  4.         Shared  Sub Main(ByVal args() As String)
  5.             Dim values()() As StringNew String()() {New String(){"0105", "Pom"}
  6. ,
  7.                     New String()
  8.                     {
  9.                         "0015", "Foo"
  10.                     }
  11. ,
  12.                     New String()
  13.                     {
  14.                         "0016", "Abc"
  15.                     }
  16. ,
  17.                     New String()
  18.                     {
  19.                         "0012", "Lkj"
  20.                     }
  21. ,
  22.                     New String()
  23.                     {
  24.                         "0001", "Tyu"
  25.                     }
  26.                 }
  27.  
  28.             Dim comparer As StringArrayComparer =  New StringArrayComparer()
  29.             comparer.CompareIndex = 0
  30.  
  31.             Array.Sort(values, comparer)
  32.  
  33.             Dim i As Integer
  34.             For  i = 0 To  values.Length- 1  Step  i + 1
  35.                 Console.Write("Row [{0}]: ", i.ToString())
  36.                     Dim j As Integer
  37.                     For  j = 0 To  values(i).Length- 1  Step  j + 1
  38.                         Console.Write("{0}\t", values(i)(j))
  39.                     Next
  40.                 Console.Write("\n")
  41.             Next
  42.  
  43.             Console.WriteLine("\nDone!")
  44.             Console.ReadLine()
  45.         End Sub
  46.     End Class
  47. '----------------------------------------------------------------
  48. ' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
  49. ' Developed by: Kamal Patel ([url]http://www.KamalPatel.net[/url])
  50. '----------------------------------------------------------------
  51.  
axion_sa is offline   Reply With Quote
Old Jan 25th, 2007, 03:30 PM   #11
pricejt
New Member
 
Join Date: Jan 07
Posts: 9
pricejt is an unknown quantity at this point (<10)
Re: [02/03] Problem Sorting 2 D Array

Ok Stanav: I take that back it work really fast but the probelm is it dosen't sort it all correctly.

07-01-15 00:00:00 00116:
07-01-15 00:00:00 00116:
07-01-15 00:00:00 00120:
07-01-15 00:00:01 00109:
07-01-15 00:00:01 00109:
07-01-15 00:00:01 00116:
07-01-15 00:00:01 00116:
07-01-15 00:00:02 00109:
07-01-15 00:00:02 00109:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:05 00109:
07-01-15 00:00:05 00109:
07-01-15 00:00:05 00119:
07-01-15 00:00:05 00120:
07-01-15 00:00:06 00121:
07-01-15 00:00:07 00119:
07-01-15 00:00:07 00119:
07-01-15 00:00:07 00119:
07-01-15 00:00:07 00120:
07-01-15 00:00:07 00120:
07-01-15 00:00:07 00120:

You can see that it sorted them all but i need it to ignore the second set of numbers the "00:00:07" and just sort on that third set. Maybe concatinate the third number onto the front of the string. but i guess it would still compare the 2nd number too.

ex. 00109 is all over the place in this file

Last edited by pricejt; Jan 25th, 2007 at 03:37 PM.
pricejt is offline   Reply With Quote
Old Jan 25th, 2007, 03:42 PM   #12
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 6,854
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: [02/03] Problem Sorting 2 D Array

Quote:
Originally Posted by pricejt
Ok Stanav: I take that back it work really fast but the probelm is it dosen't sort it all correctly.

07-01-15 00:00:00 00116:
07-01-15 00:00:00 00116:
07-01-15 00:00:00 00120:
07-01-15 00:00:01 00109:
07-01-15 00:00:01 00109:
07-01-15 00:00:01 00116:
07-01-15 00:00:01 00116:
07-01-15 00:00:02 00109:
07-01-15 00:00:02 00109:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:02 00120:
07-01-15 00:00:05 00109:
07-01-15 00:00:05 00109:
07-01-15 00:00:05 00119:
07-01-15 00:00:05 00120:
07-01-15 00:00:06 00121:
07-01-15 00:00:07 00119:
07-01-15 00:00:07 00119:
07-01-15 00:00:07 00119:
07-01-15 00:00:07 00120:
07-01-15 00:00:07 00120:
07-01-15 00:00:07 00120:

You can see that it sorted them all but i need it to ignore the second set of numbers the "00:00:07" and just sort on that third set. Maybe concatinate the third number onto the front of the string. but i guess it would still compare the 2nd number too.

ex. 00109 is all over the place in this file
With a little manipulation, you can get it to sort the way you want... Otherwise, just give me a few minutes to fix it
stanav is online now   Reply With Quote
Old Jan 25th, 2007, 04:42 PM   #13
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 6,854
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
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.             'Split the file contents by line to an array then sort it
  8.             Dim seperators As Char() = {Chr(10), Chr(13)}
  9.             Dim temps() As String = strContent.Split(seperators)
  10.             Dim strBldr As New System.Text.StringBuilder
  11.             Dim i As Integer = 0
  12.             For i = 0 To temps.GetUpperBound(0)
  13.                 If temps(i).Trim().Length >= 23 Then
  14.                     strBldr.Append(temps(i).Substring(18, 5) & temps(i) & ChrW(13))
  15.                 End If
  16.             Next
  17.             Dim lines() As String = strBldr.ToString.Split(seperators)
  18.             Array.Sort(lines)
  19.             'Write back to the file
  20.             Dim outputPath As String = strInputFilePath
  21.             If strOutputFilePath <> "" Then
  22.                 outputPath = strOutputFilePath
  23.             End If
  24.             Dim writer As IO.StreamWriter = IO.File.CreateText(outputPath)
  25.             For i = 0 To lines.GetUpperBound(0)
  26.                 writer.WriteLine(lines(i).Substring(5))
  27.             Next
  28.             writer.Close()
  29.         Catch ex As Exception
  30.             MsgBox(ex.Message)
  31.             Return False
  32.         End Try
  33.         Return True
  34.     End Function

Last edited by stanav; Jan 25th, 2007 at 04:48 PM.
stanav is online now   Reply With Quote
Old Jan 25th, 2007, 04:56 PM   #14
pricejt
New Member
 
Join Date: Jan 07
Posts: 9
pricejt is an unknown quantity at this point (<10)
Re: [02/03] Problem Sorting 2 D Array

I got the original one working by just parsing the thrid number to the front but when we threw an even bigger file at it we had memory errors.

I then tried the function you just sent and it threw an error that length cannot be less than zero

Guy im making this for also gave me a new requirement haha. After it groups those third numbers together it then has to order it by the second number which is a time code. Oh the joy.
pricejt is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 11:40 AM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.