Sorting mechanism

Experts,

I have a two dimensional array as attached. It consists of only 5 rows but in actual life it can have more than that. I need to compare the age in DataArray column 5 with the rest of the rows and sort it in descending order. I hope you can provide me with sample codes and advice.

---------------------------------------------------------
ReDim DataArray(5, 9)
    DataArray(0, 0) = "A11"
    DataArray(0, 1) = "B11"
    DataArray(0, 2) = "Jason"
    DataArray(0, 3) = "Kong"
    DataArray(0, 4) = "25"
    DataArray(0, 5) = "Married"
    DataArray(0, 6) = "Woking"
    DataArray(0, 7) = "Home"
    DataArray(0, 8) = "Alone"
   
    DataArray(1, 0) = "A12"
    DataArray(1, 1) = "B12"
    DataArray(1, 2) = "Ng"
    DataArray(1, 3) = "Lau"
    DataArray(1, 4) = "37"
    DataArray(1, 5) = "Married"
    DataArray(1, 6) = "Woking"
    DataArray(1, 7) = "Home"
    DataArray(1, 8) = "Alone"
   
    DataArray(2, 0) = "A13"
    DataArray(2, 1) = "B13"
    DataArray(2, 2) = "Dan"
    DataArray(2, 3) = "Tip"
    DataArray(2, 4) = "29"
    DataArray(2, 5) = "Married"
    DataArray(2, 6) = "Woking"
    DataArray(2, 7) = "Home"
    DataArray(2, 8) = "Alone"
   
    DataArray(3, 0) = "A14"
    DataArray(3, 1) = "B14"
    DataArray(3, 2) = "Seah"
    DataArray(3, 3) = "Onn"
    DataArray(3, 4) = "17"
    DataArray(3, 5) = "Single"
    DataArray(3, 6) = "Jobless"
    DataArray(3, 7) = "Home"
    DataArray(3, 8) = "Alone"
   
    DataArray(4, 0) = "A15"
    DataArray(4, 1) = "B15"
    DataArray(4, 2) = "Song"
    DataArray(4, 3) = "Sing"
    DataArray(4, 4) = "19"
    DataArray(4, 5) = "Married"
    DataArray(4, 6) = "Woking"
    DataArray(4, 7) = "Office"
    DataArray(4, 8) = "Double"
----------------------------------------------------------

Thanks in advanced

Jason
jason101799Asked:
Who is Participating?
 
n_narayananConnect With a Mentor Commented:
for decending order, change the loop as,

For i = 0 To 3
   For j = i + 1 To 4
       If Val(dataArray(i, 4)) < Val(dataArray(j, 4)) Then
       'Note the above change from > symbol to < symbol
           For k = 0 To 8 'Put all the elements
               temp = dataArray(i, k)
               dataArray(i, k) = dataArray(j, k)
               dataArray(j, k) = temp
           Next k
       End If
   Next j
Next i

'Print the Sorted Array
Dim strString As String

For i = 0 To 4
   strString = ""
   For j = 0 To 8
       strString = strString & dataArray(i, j) & " - "
   Next j
   Debug.Print strString
Next i
0
 
n_narayananCommented:
Here is your answer

ReDim dataArray(5, 9), SortedArray(5, 9)
   dataArray(0, 0) = "A11"
   dataArray(0, 1) = "B11"
   dataArray(0, 2) = "Jason"
   dataArray(0, 3) = "Kong"
   dataArray(0, 4) = 25
   dataArray(0, 5) = "Married"
   dataArray(0, 6) = "Woking"
   dataArray(0, 7) = "Home"
   dataArray(0, 8) = "Alone"
   
   dataArray(1, 0) = "A12"
   dataArray(1, 1) = "B12"
   dataArray(1, 2) = "Ng"
   dataArray(1, 3) = "Lau"
   dataArray(1, 4) = 37
   dataArray(1, 5) = "Married"
   dataArray(1, 6) = "Woking"
   dataArray(1, 7) = "Home"
   dataArray(1, 8) = "Alone"
   
   dataArray(2, 0) = "A13"
   dataArray(2, 1) = "B13"
   dataArray(2, 2) = "Dan"
   dataArray(2, 3) = "Tip"
   dataArray(2, 4) = 29
   dataArray(2, 5) = "Married"
   dataArray(2, 6) = "Woking"
   dataArray(2, 7) = "Home"
   dataArray(2, 8) = "Alone"
   
   dataArray(3, 0) = "A14"
   dataArray(3, 1) = "B14"
   dataArray(3, 2) = "Seah"
   dataArray(3, 3) = "Onn"
   dataArray(3, 4) = 17
   dataArray(3, 5) = "Single"
   dataArray(3, 6) = "Jobless"
   dataArray(3, 7) = "Home"
   dataArray(3, 8) = "Alone"
   
   dataArray(4, 0) = "A15"
   dataArray(4, 1) = "B15"
   dataArray(4, 2) = "Song"
   dataArray(4, 3) = "Sing"
   dataArray(4, 4) = 19
   dataArray(4, 5) = "Married"
   dataArray(4, 6) = "Woking"
   dataArray(4, 7) = "Office"
   dataArray(4, 8) = "Double"
Dim i As Integer, j As Integer, k As Integer
Dim temp

For i = 0 To 3
    For j = i + 1 To 4
        If Val(dataArray(i, 4)) > Val(dataArray(j, 4)) Then
            For k = 0 To 8 'Put all the elements
                temp = dataArray(i, k)
                dataArray(i, k) = dataArray(j, k)
                dataArray(j, k) = temp
            Next k
        End If
    Next j
Next i

'Print the Sorted Array
Dim strString As String

For i = 0 To 4
    strString = ""
    For j = 0 To 8
        strString = strString & dataArray(i, j) & " - "
    Next j
    Debug.Print strString
Next i

0
 
rspahitzCommented:
You can drop these into a recordset, then sort the recordset according to the desired column.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
jason101799Author Commented:
Rsp,

Can u explain further please. I am lost as I am new to VB.

Thanks
Jason
0
 
n_narayananCommented:
Jason

Have you worked out my solution?

It is working for me.

Narayanan
0
 
n_narayananCommented:
I am using debug.print option, you can view the Debug window by pressing Ctrl+G from Visual Basic.

The contents will be visible even after end of the Program

If you want you can use the following instead of debug.print

msgbox strString

Cheers

Narayanan
0
 
rspahitzCommented:
Hi, Jason.

First a few comments...

Try Narayanan's suggestion if your structure is not likely to change.  It has little overhead and seems to sounds code.

Since what you have is a database of information, it would be worth your while to start learning about databases.  The trouble is that DBs are extensive enough that you can spend years learning how to do them right!

Database components tend to have a lot of overhead in order to achieve the desired results.

--
If you still want to try it, you'll need to add a reference to a database object, such as the ADO reference (under projects|references.)

After that, you'll need to understand how objects work, with defininig, setting, and using.

If you don't understand either of the above, I suggest using Narayanan's idea and then start learning about References and especially objects.  If you're ready for that, let me know and I'll add more comments on showing code to build a recordset to handle your request.
0
 
jason101799Author Commented:
Great Stuff
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.