Welcome to Dream.In.Code
Become a VB Expert!

Join 149,436 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,328 people online right now. Registration is fast and FREE... Join Now!




Sorting the second column of a 2D array

 
Reply to this topicStart new topic

Sorting the second column of a 2D array

GrahamPaul
22 Jan, 2008 - 12:10 PM
Post #1

New D.I.C Head
*

Joined: 9 Dec, 2007
Posts: 9


My Contributions
Ok, so in VB my 2D array sorts by the first column. I need it to sort the second column in alphabetical order. It does not need to move/swap the columns or anything, just sort for the sake of sorting. I have searched on google etc but can't see how I can adapt to my code. Here it is:

CODE



Private Sub cmdDisplay_Click()

For I = LBound(row_index, 2) To UBound(row_index, 2) - 1
  For N = I To UBound(row_index, 2)
    If row_index(1, I) > row_index(N, I) Then
      ' Swap the array elements
    End If
  Next N
Next I


picDisplay.Cls
For row_index = 1 To 15
If types(row_index, 1) <> 0 Then
picDisplay.Print types(row_index, 1); Tab(10); types(row_index, 2)
End If
Next row_index


End Sub



As you can see I tried a sort of Bubble sort. It does not work at all, but I left it there incase it is of any use.

Thanks
GP

This post has been edited by GrahamPaul: 22 Jan, 2008 - 12:11 PM
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Sorting The Second Column Of A 2D Array
22 Jan, 2008 - 09:32 PM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
For a start, you're not swapping the elements in your actual sorting routine.

What are you trying to print to the picture box?

You've got problems here:
  • First you use row_index like an array, then you use it like a number??
  • You're trying to use the row_index array, as an index for the types array
  • And you're also trying to use it to count from 1 to 15
  • You aren't actually swapping the elements in your sort routine
  • You're not comparing the correct elements in your sort routine

OK, you sort routine should work if you change it to the following. But no promises, because I have no idea what the rest of your code looks like.

CODE

Dim temp As String
Dim i As Integer
Dim n As Integer

For i = LBound(row_index, 2) To UBound(row_index, 2) - 1
  For n = i To UBound(row_index, 2)
    If row_index(1, i) > row_index(1, n) Then
      ' Swap the array elements
      temp = row_index(1, i)
      row_index(1, i) = row_index(1, n)
      row_index(1, n) = temp
    End If
  Next n
Next i


I don't know what you're trying to achieve in the second half, but whatever it is, it won't work.
User is offlineProfile CardPM
+Quote Post

GrahamPaul
RE: Sorting The Second Column Of A 2D Array
23 Jan, 2008 - 11:16 AM
Post #3

New D.I.C Head
*

Joined: 9 Dec, 2007
Posts: 9


My Contributions
Hi, thanks for the reply. That 'sort' bit had been played around with so I know it makes hardly any sense. I implimented your code but it highlights the first line and says 'type mismatch'.

The second half was to display my array and it does work.

This is the code which adds items to my array. Don't know if this helps?

CODE
types(a, 1) = questionnumber
types(a, 2) = (Theory!Type)
a = a + 1


User is offlineProfile CardPM
+Quote Post

Nayana
RE: Sorting The Second Column Of A 2D Array
23 Jan, 2008 - 05:08 PM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
By 'it won't work' I meant that either the first half or second half would not work. Because you were being inconsistent in how you were using the same variable.

Like I said in the last post, I don't know what you were trying to use row_index as, because you used it in the sort as if it were an array, then you used it as if it was a number. So inevitably there was a type mismatch. (Either one use, or the other was correct, I didn't know which)

Change your code to this

CODE

Dim temp As String
Dim i As Integer
Dim n As Integer

' >> Sort the array
For i = LBound(types, 2) To UBound(types, 2) - 1
  For n = i To UBound(types, 2)
    If types(1, i) > types(1, n) Then
      ' Swap the array elements
      temp = types(1, i)
      types(1, i) = types(1, n)
      types(1, n) = temp
    End If
  Next n
Next i


So had two actual problems (that I know of)
  • You tried to use row_index in your sort, as if it were an array. I had no idea whether it was an array or number, so how was I supposed to know which half worked for you?
  • You didn't swap the items in the sort after comparing them.

In future, it would help us help you, if you showed the definition for each variable you use. Thanks.

Do you understand how everything works? Or do you want further explanation on something?

This post has been edited by Nayana: 23 Jan, 2008 - 05:15 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 12:19PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month