12 Replies - 3382 Views - Last Post: 07 October 2009 - 08:00 AM Rate Topic: -----

#1 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Sorting text file based on column

Posted 05 October 2009 - 10:34 PM

I have lines like this
1.0 30 Blue (2 15.50 27.0) [2 15.48 24.1] 96111 19775 100493 19608
1.0 30 Blue (2 16.00 25.0) [2 15.26 23.7] 99111 23275 101123 18112
1.0 30 Blue (2 15.12 22.0) [2 14.97 17.9] 103611 17087 109706 16065
1.0 30 Blue (2 15.50 21.0) [2 15.15 21.4] 105111 19775 104509 17325
1.0 30 Blue (2 15.00 23.0) [2 15.23 17.9] 102111 16275 109706 17876

I want to sort the column that i higlighted in accending order.

This is my code so far:
Dim FileContents() As String = IO.File.ReadAllLines(wirebot50)

		For Y As Integer = 0 To FileContents.GetUpperBound(0)

		   
			If Not FileContents(Y).Trim = "" Then


				Dim firstChar As String = Microsoft.VisualBasic.Strings.Left(FileContents(Y), 4)
				If IsNumeric(firstChar) Then
					Dim fourNumbers() As String = FileContents(Y).Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)


					Array.Sort(fourNumbers(11).ToArray)


				End If
			End If
		Next

		


		IO.File.WriteAllLines(wirebot50, FileContents)


But this does not sorting anything. What is the wrong in the above code. And if i want to sort in decending order how to modify the code? thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting text file based on column

#2 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Sorting text file based on column

Posted 05 October 2009 - 11:01 PM

I have no personal experience in this issue.
Just to throw around some ideas...
Does this work?

Dim FileContents() As String = IO.File.ReadAllLines(wirebot50)




Is that filling an array properly?
Without creating a form myself and trying it, I thought you had to specify array size and then redim to change it
Then this:


Dim fourNumbers() As String = FileContents(Y).Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)




you declare the array inside the loop without a size, first I would think you would get an error on that...even if you didn't, seems like since it is not an arraylist, you would just fill the same space in the array every time the loop....looped
I have said before, I much prefer arraylists because you don't have to declare a size.
I would iterate through the lines on my text file filling my arraylist,
then iterate through that doing your desired actions to the string data, while adding the data to a sized array so I could then use the sort method.
I hope I was helpful and not way off base. Please let me know
Was This Post Helpful? 0
  • +
  • -

#3 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: Sorting text file based on column

Posted 05 October 2009 - 11:13 PM

Thanx for reply
This is work
Dim FileContents() As String = IO.File.ReadAllLines(wirebot50)


wirebot50 is the file name that i create before so iam sure it is working. I use this for some athor methode in same program its working.


But i dont know how to use array list?
Was This Post Helpful? 0
  • +
  • -

#4 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Sorting text file based on column

Posted 05 October 2009 - 11:34 PM

Help files tell about arraylists.

Are you getting any errors?
I would set breakpoints and insure that the arrays are being filled correctly.
I think that would be the issue, if the arrays are properly set up and filled, the sort method will work
Have you looked here?
http://msdn.microsof...28VS.71%29.aspx
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 text file based on column

Posted 06 October 2009 - 04:58 AM

Here is a link is a good (for your question) implementation using Array.Sort to sort an array using some specific entry in the elements of your broken-up string. Remember that you are sorting the FileContents array of file lines (not the elements in a file line) using the 11th entry of the broken up string. You can actually break up the string in the comparer rather than before calling sort.
http://www.java2s.co...tsIComparer.htm
Was This Post Helpful? 0
  • +
  • -

#6 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: Sorting text file based on column

Posted 06 October 2009 - 06:19 PM

View Postmark.bottomley, on 6 Oct, 2009 - 03:58 AM, said:

Here is a link is a good (for your question) implementation using Array.Sort to sort an array using some specific entry in the elements of your broken-up string. Remember that you are sorting the FileContents array of file lines (not the elements in a file line) using the 11th entry of the broken up string. You can actually break up the string in the comparer rather than before calling sort.
http://www.java2s.co...tsIComparer.htm



But i just want to sort the column 11 only others all remain same. if i use Icommparer it will sort the entire column.

That why i use the coding that i post. but it not sorting. What is the wrong with the coding?
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • Joined: 29-May 08

Re: Sorting text file based on column

Posted 06 October 2009 - 06:46 PM

Map the line to a user defined class of structure.
And the sort a collection of them based on the particular mapping element.
Save
Was This Post Helpful? 0
  • +
  • -

#8 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Sorting text file based on column

Posted 06 October 2009 - 06:51 PM

Bizarre that you want to move only column 11 contents around - that would seem to disconnect that data completely from the row it is in, but the way to do that is to extract all the column 11 elements into a new array containing ONLY them and then Array.Sort should work fine. Not sure what you want to do next, but you can then write the sorted column 11 array back into the column 11 slots in FileContents. Something like the following:
Dim Column11[FileContents.Length] As String
Dim j As Integer = 0 ' Used because FileContents will have some empty lines
For i As Integer = 0 to FileContents.Length-1
  If Not FileContents(i).Trim = "" Then
	Dim firstChar As String = Microsoft.VisualBasic.Strings.Left(FileContents(i), 4)
	  If IsNumeric(firstChar) Then
		Dim fourNumbers() As String = FileContents(i).Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
		Column11(j) = fourNumbers(11)
		j = j + 1
	  End If
	End If
  End If
Next
Array.Sort(Column11)
' Now reverse the procedure to put them back


What you were trying to do was sort the 1th element of 1 split string and then do the same on the next split string. Sorting one lement is trivial and "worked", but didn't do what you want - you need to get ALL the column 11 elements before sorting.
Was This Post Helpful? 0
  • +
  • -

#9 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: Sorting text file based on column

Posted 06 October 2009 - 07:14 PM

Dim Column11[FileContents.Length] As String

can i know the purpose of the above line. Because it shows error missng "]"
then when i put like this
Dim Column11[FileContents].Length] As String

it asking for end of statement.

I want to sort column 11 then want to joint back the string and write to the file.
It can be done rite?
Was This Post Helpful? 0
  • +
  • -

#10 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: Sorting text file based on column

Posted 06 October 2009 - 07:32 PM

Sorry sorry. Acctually i need to sort the whole line. I try to use the i comparer
Was This Post Helpful? 0
  • +
  • -

#11 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: Sorting text file based on column

Posted 06 October 2009 - 11:48 PM

I simplfy the line like this
I have lines like this
400 10
200 13
300 35
300 30
100 100
100 50
50 10
45 10

I want to sort this two columns together. Like first column need to sort in decending order and second column in acendding order. Like first sort the first column in decending order
like this
400
300
300
200

since 300 got 2 then look for second column which need to sort in accending order the sorted lines will be look like this
400 10
300 30
300 35
200 13

this is my code so far
Public Class ColumnSorter
	Implements IComparer '(Of String)

	Private m_Columns() As Integer

	' Save the column numbers for sorting.
	Public Sub New(ByVal ParamArray numbers() As Integer)
		m_Columns = numbers
	End Sub

	' Compare based on the columns.
	Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
		' Split the strings into fields.
		Dim x_fields() As String = x.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
		Dim y_fields() As String = y.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)

		'Compare the columns.
		For i As Integer = 0 To m_Columns.GetUpperBound(0)
			Dim col As Integer = m_Columns(i)
					  If x_fields(col).ToString < y_fields(col).ToString Then Return 1
			If x_fields(col).ToString > y_fields(col).ToString Then Return -1
				   Next i
		Return 0

	End Function

End Class

Sub compare1()

		Dim all_text As String = My.Computer.FileSystem.ReadAllText(TextBox1.Text)
		Dim lines() As String = System.IO.File.ReadAllLines(TextBox1.Text)

		Dim column_comparer As New ColumnSorter(0)

		Array.Sort(lines, column_comparer)

		System.IO.File.WriteAllLines(TextBox1.Text, lines)

		MessageBox.Show("Done")

	End Sub

I dont know how to sort the second column also together. it only sort the first column. The result is like this

50 10
45 10
400 10
300 35
300 30
200 13
100 50
100 100

for first column the 50 and 45 should come at last but i dont know why it come first and also i dont know how to sort the second column? Can any one help me?
Was This Post Helpful? 0
  • +
  • -

#12 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: Sorting text file based on column

Posted 07 October 2009 - 12:45 AM

i change the code like this
Public Class ColumnSorter
	Implements IComparer '(Of String)

	Private m_Columns() As Integer

	' Save the column numbers for sorting.
	Public Sub New(ByVal ParamArray numbers() As Integer)
		m_Columns = numbers
	End Sub

	' Compare based on the columns.
	Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
		' Split the strings into fields.
		Dim x_fields() As String = x.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
		Dim y_fields() As String = y.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)

		'Compare the columns.
		If System.Convert.ToInt32(x_fields(11)) > System.Convert.ToInt32(y_fields(11)) Then
			Return -1
		ElseIf System.Convert.ToInt32(x_fields(11)) < System.Convert.ToInt32(y_fields(11)) Then
			Return 1
		Else
			If System.Convert.ToInt32(x_fields(12)) < System.Convert.ToInt32(y_fields(12)) Then
				Return -1
			ElseIf System.Convert.ToInt32(x_fields(12)) > System.Convert.ToInt32(y_fields(12)) Then
				Return 1
			Else
				Return 0
			End If
		End If

		Return 0


	End Function

End Class

 Dim all_text As String = My.Computer.FileSystem.ReadAllText("C:\wiresave.txt")
		Dim lines() As String = System.IO.File.ReadAllLines("C:\wiresave.txt")

		Dim column_comparer As New ColumnSorter(11, 12)

	   

		Array.Sort(lines, column_comparer)


		System.IO.File.WriteAllLines("C:\wiresave.txt", lines)

		MessageBox.Show("Done")


it working but if files are like this
------------------------------------------------------------------------------
AGILENT 3070 FIXTURE WIRING REPORT Sun Apr 19, 2009 12:47:44 PM

/var/hp3070/boards/Celestica/11_CFDIU/fixture/wires
------------------------------------------------------------------------------

Fixture Type : Agilent SimPlate Express
Fixture Size : Full
Fixture Part Number : 44200L
Top Probes Allowed : Enabled
Autofile : 33
Units : English
Wiring Method : Automatic
-----------------------------------------------------------------------------

( Pin ) [ Probe ] Length = in.

| From | To | From | To
Length|Ga|Color |(b r c )|(b r c )| X Y | X Y
------|--|------|---------------|---------------|-------|-------|-------|-------
1.0 30 Blue (2 17.50 05.0) [2 16.09 04.8] 129111 33775 129391 23939
1.0 30 Blue (2 16.50 09.0) [2 16.09 09.4] 123111 26775 122541 23939
1.0 30 Blue (2 17.00 12.0) [2 16.09 12.3] 118611 30275 118210 23939
1.0 30 Blue (2 17.00 15.0) [2 16.09 15.0] 114111 30275 114037 23939

it throw the errror saying index was outside the bounds of array. because when it read the heading all it can not split. how i can make the above code to read only this lines? :

1.0 30 Blue (2 17.50 05.0) [2 16.09 04.8] 129111 33775 129391 23939
1.0 30 Blue (2 16.50 09.0) [2 16.09 09.4] 123111 26775 122541 23939
1.0 30 Blue (2 17.00 12.0) [2 16.09 12.3] 118611 30275 118210 23939
1.0 30 Blue (2 17.00 15.0) [2 16.09 15.0] 114111 30275 114037 23939
Was This Post Helpful? 0
  • +
  • -

#13 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Sorting text file based on column

Posted 07 October 2009 - 08:00 AM

Normally the way to sort on the 2 columns would be to sort on the minor key (column12) and then on the major key (column11), but that requires what is known as a "Stable" sort - one that will NOT change the order of elements that have the same sort Key. Array.Sort is not implemented as a stable sort (most sorts like heap sort, quick sort are not stable). You will need to implement your own sort, likely a bubble sort as it can be made stable so long as you never exchange rows that have the same key.

Step one is to do an ascending sort on the column 12 entries.
Step two is to do a descending sort on the column 11 entries.
This will leave all elements sorted by column 12 in order for the same key in column 11 giving you the result you need.

You should be able to find bubble sort code easily and add calling parameters for ascending versus descending and the column to use for sorting. Bubble sort is O(n^2) which means if you double the data, it takes 4 times as long, but it does have the stability criteria.

e.g.
Sub BubbleSort (ByRef myArray As String, ByVal ascending As Boolean, ByVal sortColumn As Integer)
  For i As Integer = 0 to myArray.Length - 1
	' Create an array "keys" containing only the column(sortColumn) value
  Next
  ' Takes at most N-1 passes to sort N elements
  For i As Integer = 0 to myArray.Length - 2
	' Walk all pairs of elements
	For j As Integer = 0 to myArray.Length - 2
	  ' All swaps here swap entries in BOTH the keys and the myArray arrays
	  If ascending Then
		' if keys(j) > keys(j+1) swap myArray(j) with myArray(j+1) and swap keys(j) with keys(j+1) 
	  Else
		' if keys(j) < keys(j+1) swap myArray(j) with myArray(j+1) and swap keys(j) with keys(j+1) 
	  End If
	Next
  Next
End Sub



To get it to sort only the lines you care about, use the previous code (first post) to identify lines that are not empty and start with numbers to create an array that contains ONLY the sortable lines. They can be placed back into the original file by doing a similar check for valid lines and replace each line with the next line from your sorted numeric only lines.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1