Sorting...

Shims

Freshman
Joined
Jun 15, 2003
Messages
39
Ok, so i want to sort a whole line of information very efficiently. I am not too sure how sorting subroutines work...but i want to sort just based on the area code of a phone number which is contained in a line that has a lot more information on it....can someone point me in the right direction?
 
the data i sorted like this:

123 ab street 555-555-5555


i just want to sort it based on the first 6 of the phone number
 
This would be very complex (not difficult, just complex), you can use Regular Expressions to pin-point where each occurance of a 10 digit set of numbers are (i.e. nnn-nnn-nnnn). Load each line into a 2 dim array with the second element holding the starting positon of the phone number, then maybe do a bubble-sort.

At the top of my head this makes sense to me, perhaps someone can come up with something better.
 
Ok, i made used a quicksort to sort the information. I made an array that holds just the telephone numbers and a seperate one that holds all the other info including the phone number. Anyhow, during the process, if there is over 55,000 lines that i am sorting, the program shuts itself off without any errors... wierd. Ill try the 2 dimensional array idea though. Thanks
 
If you have already extrcted the phone number into its' own array dimension, then use that field/dimension to do your sort.
 
yes, but i need to other full array to be sorted as well... this is my code... but it shuts off the program w/o error if i have over 55,000 lines.

Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim blah As New System.Text.RegularExpressions.Regex("\d\d\d-\d\d\d-\d\d\d\d")
Dim blah2 As New System.Text.RegularExpressions.Regex("\d\d\d-\d\d\d")

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.Button3 = New System.Windows.Forms.Button()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'SaveFileDialog1
'
Me.SaveFileDialog1.FileName = "doc1"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(400, 56)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 20)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Open"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(400, 104)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 20)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Save As"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(400, 184)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(75, 20)
Me.Button3.TabIndex = 2
Me.Button3.Text = "Filter"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(160, 56)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(216, 20)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(160, 104)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(216, 20)
Me.TextBox2.TabIndex = 4
Me.TextBox2.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(624, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox2, Me.TextBox1, Me.Button3, Me.Button2, Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Public Sub Quicksort(ByVal list() As Long, ByVal str() As String, ByVal min As Long, ByVal max As Long)
Dim med_value As Long
Dim med_value2 As String
Dim hi As Long
Dim lo As Long
Dim i As Long

' If min >= max, the list contains 0 or 1 items so it
' is sorted.
If min >= max Then Exit Sub

' Pick the dividing value.
i = Int((max - min + 1) * Rnd() + min)
med_value = list(i)
med_value2 = str(i)

' Swap it to the front.
list(i) = list(min)
str(i) = str(min)

lo = min
hi = max
Do
' Look down from hi for a value < med_value.
Do While list(hi) >= med_value
hi = hi - 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
list(lo) = med_value
str(lo) = med_value2
Exit Do
End If

' Swap the lo and hi values.
list(lo) = list(hi)
str(lo) = str(hi)

' Look up from lo for a value >= med_value.
lo = lo + 1
Do While list(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
list(hi) = med_value
str(hi) = med_value2
Exit Do
End If

' Swap the lo and hi values.
list(hi) = list(lo)
str(hi) = str(lo)
Loop

' Sort the two sublists.
Quicksort(list, str, min, lo - 1)
Quicksort(list, str, lo + 1, max)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim i As Integer = -1
Dim b As Integer
Dim strReader As StreamReader = New StreamReader(New FileStream(TextBox1.Text, FileMode.Open))

Do Until strReader.Peek < 0
i += 1
strReader.ReadLine()
Loop
strReader.Close()
Dim al(i) As Long
Dim str(i) As String
b = i

strReader = New StreamReader(New FileStream(TextBox1.Text, FileMode.Open))
i = -1
Do Until strReader.Peek < 0
i += 1
str(i) = strReader.ReadLine()
Loop
strReader.Close()

Dim tempstr As String

For i = 0 To str.Length - 1
Try
tempstr = blah.Match(str(i)).Value
tempstr = blah2.Match(tempstr).Value
al(i) = tempstr.Replace("-", "")
Catch
End Try
Next
Quicksort(al, str, 0, b)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt"
OpenFileDialog1.ShowDialog()
TextBox1.Text = OpenFileDialog1.FileName.ToString
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt"
SaveFileDialog1.ShowDialog()
TextBox2.Text = SaveFileDialog1.FileName.ToString
End Sub

Private Sub Write(ByVal str() As String)
Dim strWriter As StreamWriter = New StreamWriter(New FileStream(TextBox2.Text, FileMode.CreateNew))
Dim i As Integer
For i = 0 To str.Length - 1
strWriter.WriteLine(str(i))
Next
strWriter.Close()
End Sub
End Class
 
Arrays should have a sort method.

I'd guess that this would be the fastest and most efficient way to sort an array, no ?

Most probably way faster than any "custom" sorter. If I recall correctly, there are huge differences between the different sorting algorithms. With bubble sort not being amongst the best, btw. :)
 
true, however, i need to sort a whole line of data based on the phone number, not the first character on the line. And this isnt bubble sort, it is a quicksort.
 
Back
Top