Sorting in a 2D array, VB6

Problem: Why is this code not sorting the numeric values properly?

Page 1 of 1

3 Replies - 3369 Views - Last Post: 20 February 2009 - 05:05 AM Rate Topic: -----

#1 raspsaysthewaffle   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-February 09

Sorting in a 2D array, VB6

Posted 18 February 2009 - 02:54 AM

Dim row As Integer
Dim col As Integer




MsgBox " Activate Caps Lock Now "

'Input results


For col = 0 To 8
   For row = 0 To 2
	  If row = 0 Then Call validatename(x)
	  If row = 1 Then Call validatenumber(x)
	  If row = 2 Then x = " seconds "
	  twodarray(col, row) = x
   Next row
Next col

'sort results
Do
NoSwaps = True
   For i = 0 To 8 - 1
	   If twodarray(i, 1) > twodarray((i + 1), 1) Then
	   
		   temp = twodarray((i + 1), 1)
		   twodarray((i + 1), 1) = twodarray(i, 1)
		   twodarray(i, 1) = temp
		   
		   temp2 = twodarray((i + 1), 0)
		   twodarray((i + 1), 0) = twodarray(i, 0)
		   twodarray(i, 0) = temp2
		   
		   
		   NoSwaps = False
	   End If
	 Next i
Loop Until NoSwaps



For col = 0 To 8
   For row = 0 To 2
	  Picture1.Print Tab(row * 35); twodarray(col, row);
	   Next row
	 Picture1.Print
	 Picture1.Print
Next col






This code will sort the numeric values contained in the array, however when dealing with numeric values above nine the sort algorithm seems only to register the value of the units in the array space. If anyone knows why could they please leave their suggestion as to why.

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting in a 2D array, VB6

#2 luigiplumbersinc   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: Sorting in a 2D array, VB6

Posted 18 February 2009 - 05:21 PM

i dont think i understand you problem what do you mean
"when dealing with numeric values above nine the sort algorithm seems only to register the value of the units in the array space"


as the sort runs through with 8 results (array index 0-7) checking to see if its larger than the next it should look something like

2 2 2 1 1
4 4 1 2 2
5 1 4 4 4
1 5 5 5 5
7 7 7 7 7
8 8 8 8 8
10 9 9 9 9
9 10 10 10 10
11 11 11 11 11

regardless of whether one of the values is above 9
perhaps the problem is in your validatenumber(x) procedure
im assuming x is a public variant as you assign it both string and integer values...

Goodluck :D
Was This Post Helpful? 0
  • +
  • -

#3 raspsaysthewaffle   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-February 09

Re: Sorting in a 2D array, VB6

Posted 20 February 2009 - 03:18 AM

The problem is that if the values


1 11 16 2 3 4 5 6 7

were added to this sort then the final sorted result would be

1 11 16 2 3 4 5 6 7

This seems to be because the first value of each number is only seen in the sort :(.
Was This Post Helpful? 0
  • +
  • -

#4 luigiplumbersinc   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: Sorting in a 2D array, VB6

Posted 20 February 2009 - 05:05 AM

View Postraspsaysthewaffle, on 20 Feb, 2009 - 02:18 AM, said:

The problem is that if the values


1 11 16 2 3 4 5 6 7

were added to this sort then the final sorted result would be

1 11 16 2 3 4 5 6 7

This seems to be because the first value of each number is only seen in the sort :(.


well from what i can see that means there much be a problem with the way you defined the variable "twodarray" please post this code as a suggest this would be appropriate
dim twodarray(8) as String


and then when setting score values
twodarray(index) = str(validatenumber(x))


and then when sorting the values
if Val(twodarray(i)) > Val(twodarray(i + 1)) then


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1