6 Replies - 6242 Views - Last Post: 19 June 2009 - 07:42 PM Rate Topic: -----

#1 zql  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-June 09

Array.Sort returns blank data when sorting a structure array

Post icon  Posted 19 June 2009 - 01:44 AM

I'm trying to sort an array based on a structure but whenever I do this, it basically erases all the data in the entire array.
I have verified that the array is populated correctly, it is when using array.sort that everything returns blank.

I'm trying to sort by Score

Here is the code I've written:

Module GameCode

Public Structure Score1PType
		Implements IComparable
		Public Name As String
		Public Score As Integer
		Public SDate As Date
		Public Function CompareTo(ByVal obj As Object) As Integer _
				 Implements System.IComparable.CompareTo
			 Return Me.Score.CompareTo(CType(obj, Score1P.Score)
		End Function
End Structure

Public Score1P(50) As Score1PType

Public Sub SortScores()
	 Array.Sort(Score1P)
End Sub

Any help would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Array.Sort returns blank data when sorting a structure array

#2 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Array.Sort returns blank data when sorting a structure array

Posted 19 June 2009 - 05:19 AM

First, your ctype call is wrong and second you populated your array with Score1P structures containing only the default values of each fields's type.

Here is the updated code. Note that the corrected CType call converts to the structure instead of to the Score field.

   Public Structure Score1P
		Implements IComparable
		Public Name As String
		Public Score As Integer
		Public SDate As Date
		Public Function CompareTo(ByVal obj As Object) As Integer _
				 Implements System.IComparable.CompareTo
			Return Me.Score.CompareTo(CType(obj, Score1P).Score)
		End Function
	End Structure

	Public ScoreLP(50) As Score1P
	Sub Main()
		For i As Integer = 0 To 50
			ScoreLP(i) = New Score1P With {.Score = i * New Random(500).Next(1, 10)}
		Next
		Array.Sort(ScoreLP)
	End Sub

Was This Post Helpful? 1
  • +
  • -

#3 zql  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-June 09

Re: Array.Sort returns blank data when sorting a structure array

Posted 19 June 2009 - 11:09 AM

Thank you for the corrections and advice, although this is producing the exception "Object reference not set to an instance of an object." on the New ... With {} line.
Here is the updated code I am using:

Public Structure Score1PType
   Implements IComparable
   Public Name As String
   Public Score As Integer
   Public SDate As Date
   Public Function CompareTo(ByVal obj As Object) As Integer _ 
		  Implements System.IComparable.CompareTo
	   Return Me.Score.CompareTo(CType(obj, Score1PType).Score)
	   End Function
End Structure

Public Score1P() As Score1PType

Public Sub LoadScores()
   
   Dim LoadedName as String
   Dim LoadedDate as Date
   Dim LoadedScore as Integer
   Dim ScoreCounter as Integer

   .... (code omitted)

   Do Until Eof(1)
		LoadedName = LineInput(1)
		LoadedScore = LineInput(1)
		LoadedDate = LineInput(1)
		
		Score1P(ScoreCounter) = New Score1PType With {.Name = LoadedName, _ 
			   .Score = LoadedScore, .SDate = LoadedDate}

		ScoreCounter += 1
		ReDim Preserve Score1P(ScoreCounter)
   Loop

   Array.Sort(Score1P)



View PostT3hC13h, on 19 Jun, 2009 - 04:19 AM, said:

First, your ctype call is wrong and second you populated your array with Score1P structures containing only the default values of each fields's type.

Here is the updated code. Note that the corrected CType call converts to the structure instead of to the Score field.

Was This Post Helpful? 0
  • +
  • -

#4 zql  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-June 09

Re: Array.Sort returns blank data when sorting a structure array

Posted 19 June 2009 - 11:53 AM

Okay, with your help, I was finally able to nail this. I understand now why you were populating the array with the random numbers.. as that is the only way this would have worked.
After the random numbers, I'm then free to insert correct data in a second loop and then redim the array to trim it down before sorting. It works perfectly.. thank you again!
Was This Post Helpful? 0
  • +
  • -

#5 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Array.Sort returns blank data when sorting a structure array

Posted 19 June 2009 - 12:06 PM

View Postzql, on 19 Jun, 2009 - 10:53 AM, said:

Okay, with your help, I was finally able to nail this. I understand now why you were populating the array with the random numbers.. as that is the only way this would have worked.
After the random numbers, I'm then free to insert correct data in a second loop and then redim the array to trim it down before sorting. It works perfectly.. thank you again!



I used the random numbers to test the implementation of the ICompareable Interface and the function of Array.Sort. If you post your code I'll try and see if I can eliminated the second loop by loading the data the first time.
Was This Post Helpful? 0
  • +
  • -

#6 zql  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-June 09

Re: Array.Sort returns blank data when sorting a structure array

Posted 19 June 2009 - 02:50 PM

That's what I get for writing code as soon as I wake up in the morning. The problem was that I needed to ReDim prior to adding my data (which was done during the random number generation). With this, the need for a second loop is eliminated and the array is only as large as it needs to be prior to sorting.

View PostT3hC13h, on 19 Jun, 2009 - 11:06 AM, said:

View Postzql, on 19 Jun, 2009 - 10:53 AM, said:

Okay, with your help, I was finally able to nail this. I understand now why you were populating the array with the random numbers.. as that is the only way this would have worked.
After the random numbers, I'm then free to insert correct data in a second loop and then redim the array to trim it down before sorting. It works perfectly.. thank you again!



I used the random numbers to test the implementation of the ICompareable Interface and the function of Array.Sort. If you post your code I'll try and see if I can eliminated the second loop by loading the data the first time.

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Array.Sort returns blank data when sorting a structure array

Posted 19 June 2009 - 07:42 PM

Problem i believe is that you're not creating the IComparer.Compare correctly.
Custom Sorting
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1