Solved

Sorting mechanism

Posted on 2002-04-23
174 Views
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"
----------------------------------------------------------

Jason
0
Question by:jason101799
• 4
• 2
• 2

LVL 3

Expert Comment

ID: 6964652

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

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

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

Author Comment

ID: 6967553
Rsp,

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

Thanks
Jason
0

LVL 3

Expert Comment

ID: 6967570
Jason

Have you worked out my solution?

It is working for me.

Narayanan
0

LVL 3

Expert Comment

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

ID: 6969148
Hi, Jason.

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

ID: 6994138
Great Stuff
0

Featured Post

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 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…