Solved

Sorting mechanism

Posted on 2002-04-23
8
182 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…
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…

856 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