?
Solved

How to sort a 2 dimensional array based on dimension 1

Posted on 2005-02-28
11
Medium Priority
?
251 Views
Last Modified: 2010-05-02
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
 
Please help.

Thanks,

Dovberman

0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 21

Expert Comment

by:oleggold
ID: 13422579
0
 

Author Comment

by:Dovberman
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

by:Mike Tomlinson
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

Private Sub Form_Load()
    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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:fatalXception
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

by:Carl Tawn
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

Private Sub Form_Load()

    '// 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

by:Dovberman
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.

Your solution works perfect.

Dovberman
0
 

Author Comment

by:Dovberman
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

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

Expert Comment

by:fatalXception
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

by:Dovberman
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

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

"AAAdministraaaator!"
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question