Solved

Sorting mechanism

Posted on 2002-04-23
8
191 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jason101799
ID: 6967553
Rsp,

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

Thanks
Jason
0
 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

710 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