Solved

Sorting mechanism

Posted on 2002-04-23
8
176 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:jason101799
  • 4
  • 2
  • 2
8 Comments
 
LVL 3

Expert Comment

by:n_narayanan
ID: 6964652
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
 
LVL 3

Accepted Solution

by:
n_narayanan earned 100 total points
ID: 6964706
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 6966375
You can drop these into a recordset, then sort the recordset according to the desired column.
0
 

Author Comment

by:jason101799
ID: 6967553
Rsp,

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

Thanks
Jason
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:n_narayanan
ID: 6967570
Jason

Have you worked out my solution?

It is working for me.

Narayanan
0
 
LVL 3

Expert Comment

by:n_narayanan
ID: 6967575
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 6969148
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
 

Author Comment

by:jason101799
ID: 6994138
Great Stuff
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now