473,900 Members | 4,485 Online

# Sorting 2D-array

132 New Member
Hello,

I'm trying to get my 2D-array sorted but for some reason I can't seem to get it sorted in the right order. Anybody that can help me find the solution?

I have an array which has been constructed as follows
Expand|Select|Wrap|Line Numbers
1. array (Name, Number1, Number2, Total, Previous Position, New Position)
2.
I'm using the last two columns as a way to define the order.
The value that needs to be sorted is the 4th column (Total).
But instead of actually changing the indexes, I want to change the value of the column New Position.

So to give you an example:

BEFORE SORT
Expand|Select|Wrap|Line Numbers
1. person(0) ("NAME1", 4, 51, 55,1,1)
2. person(1) ("NAME2", 12, 41, 53,2,2)
3. person(2) ("NAME3", 24, 35, 59,3,3)
4. person(3) ("NAME4", 1, 11, 12,4,4)
5. person(4) ("NAME5", 0, 22, 22,5,5)
6.
AFTER SORT
Expand|Select|Wrap|Line Numbers
1. person(0) ("NAME1", 4, 51, 55,1,2)
2. person(1) ("NAME2", 12, 41, 53,2,3)
3. person(2) ("NAME3", 24, 35, 59,3,1)
4. person(3) ("NAME4", 1, 11, 12,4,5)
5. person(4) ("NAME5", 0, 22, 22,5,4)
6.
As you can see, only the value in the last column has changed depending on the value of the 4th column.

I'm using this last column to define the order in which I'm displaying this array. But I need to be able to keep track of the original position (the second to last column).

Right now I'm using the following code:

Expand|Select|Wrap|Line Numbers
1. Dim Pos1 As Integer
2. Dim Pos2 As Integer
3.
4.     For i = 0 To (UBound(Person) - 1)
5.
6.         'Previous Positions
7.         Person(i, 4) = Person(i, 5)
8.
9.         For j = i To UBound(Person)
10.             If CInt(Person(i, 3)) < CInt(Person(j, 3)) Then
11.                 Pos1 = CInt(Person(i, 5))
12.                 Pos2 = CInt(Person(j, 5))
13.
14.
15.                 ' New Positions
16.                 Person(i, 5) = Pos2
17.                 Person(j, 5) = Pos1
18.
19.             End If
20.         Next
21.     Next
22.
Thanks for the help!
Nov 16 '12
12 3235
Rabbit
12,516 Recognized Expert Moderator MVP
I was under the impression that the starting positions matched the index. I got this impression from your sample data in posts #1, #3, #7. The algorithm I gave you only works using that assumption.

If, however, that is not the case, then you can easily make the data conform by setting the positions to the index before doing the sort.
Nov 23 '12 #11
Cainnech
132 New Member
Unfortunately, I can't change my index as I need to keep my Persons-array in the same order. The only way I can see that happening is if I add yet another dimension to the array which has the order in which it should appear.

In effect I don't think it will be possible to sort this array directly from itself. The only solution that I see now is to use a control that does the sorting for me.
Nov 23 '12 #12
Rabbit
12,516 Recognized Expert Moderator MVP
I didn't say you need to change the index. Just change the position to the index after setting the previous position and before the sort.
Nov 24 '12 #13