6 Replies - 4717 Views - Last Post: 15 May 2009 - 08:31 AM Rate Topic: -----

#1 mlegendre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-May 09

Sorting a text file in VB 2008

Post icon  Posted 04 May 2009 - 09:31 AM

Ok so I know that you can sort by using a bunch of different sorting algorithms but I just am not sure how to start this off. I have written this code and just want to know if you can even sort this.

The program takes the users information in from text boxes from another part of the program and writes the information into a text file. So when the user clicks on this button it will display all the people that have entered in their names. I want to sort their last names alphabetticaly.



Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListCustomers.Click

		'Variables
		Dim sr As StreamReader = File.OpenText("test.txt")
		Dim fmtStr As String = "{0,-15}{1,10}  {2,10}	{3,5}"
		Dim strLastName, strFirstName As String
		Dim intZip, intID As Integer

		'Output header for previous users
		lstOutput.Items.Clear()
		lstOutput.Items.Add(String.Format(fmtStr, "Last Name", "First Name", "Zip Code", "ID #"))
		lstOutput.Items.Add("")

		'Read previous users information
		Do Until sr.Peek = -1
			strLastName = (sr.ReadLine)
			strFirstName = (sr.ReadLine)
			intZip = (sr.ReadLine)
			intID = (sr.ReadLine)

			lstOutput.Items.Add(String.Format(fmtStr, strLastName, strFirstName, intZip, intID))
		Loop
		sr.Close()
	End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Sorting a text file in VB 2008

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Sorting a text file in VB 2008

Posted 04 May 2009 - 09:32 AM

VB 2008 is VB.NET, not VB6. Moved :)
Was This Post Helpful? 0
  • +
  • -

#3 mlegendre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-May 09

Re: Sorting a text file in VB 2008

Posted 04 May 2009 - 09:35 AM

View PostPsychoCoder, on 4 May, 2009 - 08:32 AM, said:

VB 2008 is VB.NET, not VB6. Moved :)


Roger, thanks!
Was This Post Helpful? 0
  • +
  • -

#4 LemonMan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 170
  • Joined: 22-August 05

Re: Sorting a text file in VB 2008

Posted 05 May 2009 - 04:26 PM

Try this

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListCustomers.Click

		'Variables
		Dim sr As StreamReader = File.OpenText("test.txt")
		Dim fmtStr As String = "{0,-15}{1,10}  {2,10}	{3,5}"
		Dim strLastName, strFirstName As String
		Dim intZip, intID As Integer

		'Output header for previous users
		lstOutput.Items.Clear()
		lstOutput.Items.Add(String.Format(fmtStr, "Last Name", "First Name", "Zip Code", "ID #"))
		lstOutput.Items.Add("")
Dim myPeeps As New List(Of String)
		'Read previous users information
		Do Until sr.Peek = -1
			strLastName = (sr.ReadLine)
			strFirstName = (sr.ReadLine)
			intZip = (sr.ReadLine)
			intID = (sr.ReadLine)

			myPeeps.Add(String.Format(fmtStr, strLastName, strFirstName, intZip, intID))
		Loop

myPeeps.Sort()
For Each strPerson As String In myPeeps
lstOutput.Items.Add(strPerson)
Next
		sr.Close()
	End Sub


Was This Post Helpful? 0
  • +
  • -

#5 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Sorting a text file in VB 2008

Posted 05 May 2009 - 04:59 PM

You should be able to sort it by just enabling the "ListBox1.Sorted = True". The problem is the labels and the blank line would also be sorted so maybe just position them in a label outside the box or add them at the beginning after loading the data and then disabling sorted.
Was This Post Helpful? 0
  • +
  • -

#6 mlegendre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-May 09

Re: Sorting a text file in VB 2008

Posted 14 May 2009 - 08:17 AM

View PostLemonMan, on 5 May, 2009 - 03:26 PM, said:

Try this

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListCustomers.Click

		'Variables
		Dim sr As StreamReader = File.OpenText("test.txt")
		Dim fmtStr As String = "{0,-15}{1,10}  {2,10}	{3,5}"
		Dim strLastName, strFirstName As String
		Dim intZip, intID As Integer

		'Output header for previous users
		lstOutput.Items.Clear()
		lstOutput.Items.Add(String.Format(fmtStr, "Last Name", "First Name", "Zip Code", "ID #"))
		lstOutput.Items.Add("")
Dim myPeeps As New List(Of String)
		'Read previous users information
		Do Until sr.Peek = -1
			strLastName = (sr.ReadLine)
			strFirstName = (sr.ReadLine)
			intZip = (sr.ReadLine)
			intID = (sr.ReadLine)

			myPeeps.Add(String.Format(fmtStr, strLastName, strFirstName, intZip, intID))
		Loop

myPeeps.Sort()
For Each strPerson As String In myPeeps
lstOutput.Items.Add(strPerson)
Next
		sr.Close()
	End Sub




Thanks Ill check that out.

View Postmark.bottomley, on 5 May, 2009 - 03:59 PM, said:

You should be able to sort it by just enabling the "ListBox1.Sorted = True". The problem is the labels and the blank line would also be sorted so maybe just position them in a label outside the box or add them at the beginning after loading the data and then disabling sorted.



I have tried doing the sort properties on the list box but like you said it sorts everything in the list box. I could put it in as the program loads, but thats not what im going for. I am trying to figure out the different sorting algorithms, the easier ones first so bubblesort. Thanks for the tip though. I appreciate all your help.
Was This Post Helpful? 0
  • +
  • -

#7 Poppa Mintin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 13-September 08

Re: Sorting a text file in VB 2008

Posted 15 May 2009 - 08:31 AM

Mlegendre…

Just a thought about sorting by family name…
Maybe try reversing the order of each name,
Store them in a ListBox with Sorted = True

Make a Function: -
Say: - Private Function Reverse (N1 as String) as string
Give yourself some local variables
Pass each name in a string
  Private Function Reverse(ByVal N1 As String) As String
		Dim a As Integer, z, y As String

		a = InStr(N1, " ")
		y = ""
		While a > 0
			z = Mid$(N1, 1, a)
			N1 = Mid$(N1, a + 1)
			a = InStr(N1, " ")
			If y > "" Then
				y = z + " " + y
			Else
				y = z
			End If
		End While

		Reverse = N1 + " " + y
	End Function



a = InStr(N1, " ") will set Variable a to the position of the first space in string N1 or return a 0 if there isn’t a space.

z = Mid$(N1, 1, a) will set Variable z to a string of the characters from position 1 to the length set in Variable a. (Note the 2 perameters, where to start, how many characters)

N1 = Mid$(N1, a + 1) will reduce N1 by the part previously read. (Note just 1 perameter a+1, where to start, and get the rest)

y is used as a store for each name part as it is found, and by adding that part before the previous parts will reverse their order.
(Note that you don’t want a space after the first part found)

Reverse = N1 + " " + y: - Note also that when no space is found you still need to add the last part to the result.

The subroutine will finish when the individual parts of N1 are in reverse order. I.e. Fred J Blogs would become Blogs J Fred.

If you put the reversed names into the ListBox they will be sorted alphabetically by family name.

Then pull each name back out and run it through the Function again.


Poppa

This post has been edited by Poppa Mintin: 15 May 2009 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1