Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# How to sort a 2 dimensional array based on dimension 1

Posted on 2005-02-28
Medium Priority
256 Views
I have been trying for 6 hours to sort an array.

Example

Dim arr(1 to 5, 1 to 2) as Integer  (Rows 1 to 5, Cols 1 to 2)  'Source
Dim arrS(1 to 5, 1 to 2) as Integer  (Rows 1 to 5, Cols 1 to 2) 'Target

'Here is the unsorted array, arr()
arr(1,1)=2
arr(1,2)=7
arr(2,1)=5
arr(2,2)=9
arr(3,1)=4
arr(3,2)=12
arr(4,1)=12
arr(4,2)=15
arr(5,1)=1
arr(5,2)=6

'I need to sort the array and copy the results to a second array, arrS() based on the row value and copy the source col value as follows
arrS(1,1)=1
arrS(1,2)=6

arrS(2,1)=2
arrS(2,2)=7

arrS(3,1)=4
arrS(3,2)=12

arrS(4,1)=5
arrS(4,2)=9

arrS(5,1)=12
arrS(5,2)=15

Thanks,

Dovberman

0
Question by:Dovberman

LVL 21

Expert Comment

ID: 13422579
0

Author Comment

ID: 13422668
I am programming in VB6. The article refers to VB.NET.  I do not need to build a class.

What is available in VB6 ?

Thanks,

Dovberman
0

LVL 86

Expert Comment

ID: 13422935
Option Explicit

Dim arr(1 To 5, 1 To 2) As Integer  ' (Rows 1 to 5, Cols 1 to 2)  'Source
Dim arrS(1 To 5, 1 To 2) As Integer  ' (Rows 1 to 5, Cols 1 to 2) 'Target

arr(1, 1) = 2
arr(1, 2) = 7
arr(2, 1) = 5
arr(2, 2) = 9
arr(3, 1) = 4
arr(3, 2) = 12
arr(4, 1) = 12
arr(4, 2) = 15
arr(5, 1) = 1
arr(5, 2) = 6
End Sub

Private Sub Command1_Click()
sortArray arr, arrS

Debug.Print "arr:"
displayArray arr

Debug.Print
Debug.Print "arrs:"
displayArray arrS
End Sub

Private Sub displayArray(ByRef source As Variant)
Dim i As Integer
Dim j As Integer
For i = LBound(source, 1) To UBound(source, 1)
For j = LBound(source, 2) To UBound(source, 2)
Debug.Print "(" & i & ", " & j & ") = " & source(i, j)
Next j
Next i
End Sub

Private Sub sortArray(ByRef source As Variant, ByRef target As Variant)
Dim i As Integer
Dim j As Integer
Dim k
Dim tempValue As Integer
Dim tempIndex As Integer

' Make sure the arrays have the same dimensions
On Error Resume Next
If LBound(source, 1) <> LBound(target, 1) Then
MsgBox "Source and Target Arrays must be the same size", vbCritical, "Invalid Target Array"
Exit Sub
End If
If LBound(source, 2) <> LBound(target, 2) Then
MsgBox "Source and Target Arrays must be the same size", vbCritical, "Invalid Target Array"
Exit Sub
End If
If UBound(source, 1) <> UBound(target, 1) Then
MsgBox "Source and Target Arrays must be the same size", vbCritical, "Invalid Target Array"
Exit Sub
End If
If UBound(source, 2) <> UBound(target, 2) Then
MsgBox "Source and Target Arrays must be the same size", vbCritical, "Invalid Target Array"
Exit Sub
End If
On Error GoTo 0

' Copy the source array into the target array
For i = LBound(source, 1) To UBound(source, 1)
For j = LBound(source, 2) To UBound(source, 2)
target(i, j) = source(i, j)
Next j
Next i

' bubble sort 'em
For i = (UBound(target, 1) - 1) To LBound(target, 1) Step -1
For j = LBound(target, 1) To i
If target(j, 1) > target(j + 1, 1) Then
' swap values
For k = LBound(target, 2) To UBound(target, 2)
tempValue = target(j, k)
target(j, k) = target(j + 1, k)
target(j + 1, k) = tempValue
Next k
End If
Next j
Next i
End Sub

0

LVL 8

Expert Comment

ID: 13423053
Could you expand the array into a one-dimensional array, sort it, and then pack it back into a 2D array?
so your one-D array would look like
2,7,5,9,4,12,12,15,1,6
Sort it however (bubble sort let's say)
1,2,4,5,6,7,9,12,12,15

To translate this into a 2D array again, you'll need to work out the mapping
theArray index ->2d index
1 ->1,1
2->1,2
3->2,1
4->2,2

and so on. Sorry but Im not that good at translations but it looks like it might be handy enough?
0

LVL 52

Expert Comment

ID: 13423104
I know Idle_Mind has already posted an adequate solution already, but i'd already written this by then, so here it is anyway :o)

Dim arr(1 To 5, 1 To 2) As Integer
Dim arrS(1 To 5, 1 To 2) As Integer

'// Here is the unsorted array, arr()
arr(1, 1) = 2
arr(1, 2) = 7
arr(2, 1) = 5
arr(2, 2) = 9
arr(3, 1) = 4
arr(3, 2) = 12
arr(4, 1) = 12
arr(4, 2) = 15
arr(5, 1) = 1
arr(5, 2) = 6

Call SortArray(1)
Call SortArray(2)

End Sub

Private Sub SortArray(ByVal pElement As Integer)

Dim i As Integer
Dim j As Integer
Dim x As Integer

For i = UBound(arr) - 1 To LBound(arr) Step -1
For j = UBound(arr) To LBound(arr) Step -1

If CInt(arr(j, pElement)) < CInt(arr(i, pElement)) Then
'// Swap elements
x = arr(j, pElement)
arr(j, pElement) = arr(i, pElement)
arr(i, pElement) = x
End If

Next j
Next i

For i = LBound(arr) To UBound(arr)
arrS(i, pElement) = arr(i, pElement)
Next i

End Sub
0

Author Comment

ID: 13423267
Thanks,

The key is in the bubble sort to bring the unsorted dimension value into the sorted array.

I was trying to use SelectionSortNumbers which is a completely different technique.

Dovberman
0

Author Comment

ID: 13423476
I wish to thank both FatalXception and Carl Tawn for their quick replies.

I am sure that both variations will work.  However, I have already tested and applied the answer from Idle Mind.

Dovberman
0

LVL 8

Accepted Solution

fatalXception earned 1000 total points
ID: 13427094
cool, as long as you got it working.
0

LVL 8

Expert Comment

ID: 13591116
Are you sure you meant to give me the points?
Not that I'm complaining, mind, but if Idle Mind's worked then I reckon he is due the points, or at least some of them?

Idle_Mind, what do you think?
0

Author Comment

ID: 13592762
I was not aware that the points went to the person whose answer was accepted.

Tell me how to re-open the issue, and I will split points.

Thanks,

dovberman
0

LVL 8

Expert Comment

ID: 13592879
"Meeediiiiic!"
ahem, sorry sorry....

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asseâ€¦
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describesâ€¦
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can launâ€¦
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This lâ€¦
###### Suggested Courses
Course of the Month11 days, 10 hours left to enroll