473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Sorting 2D-array

132 100+
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 #1

✓ answered by Rabbit

On line 53, you need to go all the way to the end of the array, not array -1.

On line 54, you need to start from index 0, not index i.

If you want a descending sort, you need to flip the comparison on your current position.

12 3198
Rabbit
12,516 Expert Mod 8TB
What are you getting instead of what you want?
Nov 16 '12 #2
Cainnech
132 100+
Right now I'm getting something like this:

person(0) ("NAME1", 4, 51, 55,1,5)
person(1) ("NAME2", 12, 41, 53,2,3)
person(2) ("NAME3", 24, 35, 59,3,1)
person(3) ("NAME4", 1, 11, 12,4,2)
person(4) ("NAME5", 0, 22, 22,5,4)

It's not being sorted well.
Nov 16 '12 #3
Rabbit
12,516 Expert Mod 8TB
1) You should set the previous positions for all elements before doing any sorting. Otherwise, it won't come out correct.

2) You are swapping the positions if one value is larger than the other. But you only swap if the position is also larger. You can't swap them everytime otherwise you might swap them into the wrong position.
Nov 16 '12 #4
Cainnech
132 100+
Hi Rabbit,

Before I start sorting I already syncronised the old positions with the new ones.

I've tried your suggestion and added a condition where the compared position must be smaller before I actually swap the positions but still I don't get the right order.

If I try to sort like this using controls, it works like a charm. But I can't seem to make the switch to do the same in an array.
Nov 18 '12 #5
Rabbit
12,516 Expert Mod 8TB
1) Actually, if you look in your code above, you're not synchronizing everything before doing the sorting. It's synchronizing in between each element.

2) It would help to see your newly modified code in addition to the new results.
Nov 19 '12 #6
Cainnech
132 100+
Trust me, I'm syncronizing the values :-) The sample-code was indeed wrong, actually in the actual code I'm syncronizing before I start the sort.

But in order for you to check the code I've simplified it to this code:

Expand|Select|Wrap|Line Numbers
  1. Dim Persons(4, 3) As String
  2.  
  3.  
  4. Private Sub cmdDebug_Click()
  5.     For i = 0 To UBound(Persons)
  6.         Debug.Print Persons(i, 0) & " | " & Persons(i, 1) & " | " & Persons(i, 2) & " | " & Persons(i, 3)
  7.     Next
  8.  
  9.     Debug.Print "-------------------------------------------------------"
  10. End Sub
  11.  
  12. Private Sub cmdSort_Click()
  13.     SortPersons
  14. End Sub
  15.  
  16. Private Sub Form_Load()
  17.  
  18.     ResetPersons
  19.  
  20. End Sub
  21.  
  22. Public Sub ResetPersons()
  23.     Persons(0, 0) = "NAME1"
  24.     Persons(0, 1) = 1
  25.     Persons(0, 2) = 1
  26.     Persons(0, 3) = 20
  27.  
  28.     Persons(1, 0) = "NAME2"
  29.     Persons(1, 1) = 2
  30.     Persons(1, 2) = 2
  31.     Persons(1, 3) = 50
  32.  
  33.     Persons(2, 0) = "NAME3"
  34.     Persons(2, 1) = 3
  35.     Persons(2, 2) = 3
  36.     Persons(2, 3) = 40
  37.  
  38.     Persons(3, 0) = "NAME4"
  39.     Persons(3, 1) = 4
  40.     Persons(3, 2) = 4
  41.     Persons(3, 3) = 30
  42.  
  43.     Persons(4, 0) = "NAME5"
  44.     Persons(4, 1) = 5
  45.     Persons(4, 2) = 5
  46.     Persons(4, 3) = 10
  47.  
  48. End Sub
  49.  
  50. Public Sub SortPersons()
  51. Dim TempPos As Integer
  52.  
  53.     For i = 0 To (UBound(Persons) - 1)
  54.         For n = i To UBound(Persons)
  55.             If Persons(i, 3) > Persons(n, 3) And Persons(i, 2) < Persons(n, 2) Then
  56.                 TempPos = Persons(i, 2)
  57.                 Persons(i, 2) = Persons(n, 2)
  58.                 Persons(n, 2) = TempPos
  59.             End If
  60.         Next
  61.     Next
  62. End Sub
  63.  
Nov 22 '12 #7
Rabbit
12,516 Expert Mod 8TB
On line 53, you need to go all the way to the end of the array, not array -1.

On line 54, you need to start from index 0, not index i.

If you want a descending sort, you need to flip the comparison on your current position.
Nov 22 '12 #8
Cainnech
132 100+
Thanks Rabbit,

After your remarks, it seems to be working right now. Although I don't understand completely why... :-)

But the important thing is that it works!
Thanks Rabbit!
Nov 22 '12 #9
Cainnech
132 100+
It seems I was a bit premature with the solution because I'm still having issues.

When using this code:
Expand|Select|Wrap|Line Numbers
  1. Dim Persons(9, 6) As String
  2.  
  3.  
  4. Private Sub cmdDebug_Click()
  5.     For i = 0 To UBound(Persons)
  6.         Debug.Print Persons(i, 0) & " | " & Persons(i, 1) & " | " & Persons(i, 2) & " | " & Persons(i, 3) & " | " & Persons(i, 4) & " | " & Persons(i, 5) & " | " & Persons(i, 6)
  7.     Next
  8.  
  9.     Debug.Print "-------------------------------------------------------"
  10. End Sub
  11.  
  12. Private Sub cmdSort_Click()
  13.     SortPersons
  14. End Sub
  15.  
  16. Private Sub Form_Load()
  17.  
  18.     ResetPersons
  19.  
  20. End Sub
  21.  
  22. Public Sub ResetPersons()
  23.     Persons(0, 0) = "NAME1"
  24.     Persons(0, 1) = 0
  25.     Persons(0, 2) = 0
  26.     Persons(0, 3) = 0
  27.     Persons(0, 4) = 0
  28.     Persons(0, 5) = 5
  29.     Persons(0, 6) = 5
  30.  
  31.     Persons(1, 0) = "NAME2"
  32.     Persons(1, 1) = 0
  33.     Persons(1, 2) = 0
  34.     Persons(1, 3) = 0
  35.     Persons(1, 4) = 0
  36.     Persons(1, 5) = 2
  37.     Persons(1, 6) = 2
  38.  
  39.     Persons(2, 0) = "NAME3"
  40.     Persons(2, 1) = 25
  41.     Persons(2, 2) = 0
  42.     Persons(2, 3) = 0
  43.     Persons(2, 4) = 25
  44.     Persons(2, 5) = 4
  45.     Persons(2, 6) = 4
  46.  
  47.     Persons(3, 0) = "NAME4"
  48.     Persons(3, 1) = 0
  49.     Persons(3, 2) = 0
  50.     Persons(3, 3) = 0
  51.     Persons(3, 4) = 0
  52.     Persons(3, 5) = 1
  53.     Persons(3, 6) = 1
  54.  
  55.     Persons(4, 0) = "NAME5"
  56.     Persons(4, 1) = 0
  57.     Persons(4, 2) = 0
  58.     Persons(4, 3) = 0
  59.     Persons(4, 4) = 0
  60.     Persons(4, 5) = 10
  61.     Persons(4, 6) = 10
  62.  
  63.     Persons(5, 0) = "NAME6"
  64.     Persons(5, 1) = 0
  65.     Persons(5, 2) = 0
  66.     Persons(5, 3) = 0
  67.     Persons(5, 4) = 0
  68.     Persons(5, 5) = 6
  69.     Persons(5, 6) = 6
  70.  
  71.     Persons(6, 0) = "NAME7"
  72.     Persons(6, 1) = 0
  73.     Persons(6, 2) = 0
  74.     Persons(6, 3) = 0
  75.     Persons(6, 4) = 0
  76.     Persons(6, 5) = 8
  77.     Persons(6, 6) = 8
  78.  
  79.     Persons(7, 0) = "NAME8"
  80.     Persons(7, 1) = 32
  81.     Persons(7, 2) = 0
  82.     Persons(7, 3) = 0
  83.     Persons(7, 4) = 32
  84.     Persons(7, 5) = 9
  85.     Persons(7, 6) = 9
  86.  
  87.     Persons(8, 0) = "NAME9"
  88.     Persons(8, 1) = 20
  89.     Persons(8, 2) = 0
  90.     Persons(8, 3) = 0
  91.     Persons(8, 4) = 20
  92.     Persons(8, 5) = 7
  93.     Persons(8, 6) = 7
  94.  
  95.     Persons(9, 0) = "NAME10"
  96.     Persons(9, 1) = 0
  97.     Persons(9, 2) = 0
  98.     Persons(9, 3) = 0
  99.     Persons(9, 4) = 0
  100.     Persons(9, 5) = 3
  101.     Persons(9, 6) = 3
  102.  
  103. End Sub
  104.  
  105. Public Sub SortPersons()
  106. Dim TempPos As Integer
  107.  
  108.     For i = 0 To UBound(Persons)
  109.         For n = 0 To UBound(Persons)
  110.             If Persons(i, 4) > Persons(n, 4) And CInt(Persons(i, 6)) > CInt(Persons(n, 6)) Then
  111.                 TempPos = Persons(i, 6)
  112.                 Persons(i, 6) = Persons(n, 6)
  113.                 Persons(n, 6) = TempPos
  114.             End If
  115.         Next
  116.     Next
  117. End Sub
  118.  
The sort isn't being carried correctly.

The result I'm getting is:

NAME1 | 0 | 0 | 0 | 0 | 5 | 9
NAME2 | 0 | 0 | 0 | 0 | 2 | 7
NAME3 | 25 | 0 | 0 | 25 | 4 | 4
NAME4 | 0 | 0 | 0 | 0 | 1 | 5
NAME5 | 0 | 0 | 0 | 0 | 10 | 10
NAME6 | 0 | 0 | 0 | 0 | 6 | 6
NAME7 | 0 | 0 | 0 | 0 | 8 | 8
NAME8 | 32 | 0 | 0 | 32 | 9 | 1
NAME9 | 20 | 0 | 0 | 20 | 7 | 2
NAME10 | 0 | 0 | 0 | 0 | 3 | 3

Name10 is being considered as the 3rd highest number while it should actually be Name3.

Any suggestions?
Nov 23 '12 #10
Rabbit
12,516 Expert Mod 8TB
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 100+
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 Expert Mod 8TB
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

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: John Hunter | last post by:
I have a list of two tuples containing x and y coord (x0, y0) (x1, y1) ... (xn, yn) Given a new point x,y, I would like to find the point in the list closest to x,y. I have to do this a...
4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
0
by: beliavsky | last post by:
xiaohua_sun@yahoo.com (SunX) wrote in message news:<337e6cd5.0405291411.4376debc@posting.google.com>... > What is the best way to assign a 2d lists? Something like; > > for i in range(10): > ...
2
by: Oldchatterman | last post by:
Hello, in an application I measure a lot of 2d coordinates (x,y) of a pattern. This pattern consists of a set of points on grid with fixed pitches in x and y direction. These coordinates all...
0
by: Simena Dinas | last post by:
KToon: 2D Animation Toolkit KToon is a 2D Animation Toolkit designed by animators (Toonka Films) for animators, focused to the Cartoon's industry. This project is covered by the GPL License...
2
by: Shamli | last post by:
I am looking for an algorithm that enlarge a 2D polygon. cheers,
23
by: yatindran | last post by:
hai this is my 2d array. int a = { {5,2,20,1,30,10}, {23,15,7,9,11,3}, {40,50,34,24,14,4}, {9,10,11,12,13,14}, {31,4,18,8,27,17}, {44,32,13,19,41,19}, {1,2,3,4,5,6},
1
by: Jesper | last post by:
Hi, 1. I've search the net for some great usercontrols where I can plot data, draw graphs in both 2d and 3d. I've not had that much of success in my search, and I'm considering writing my own....
5
by: Jack Nielsen | last post by:
Does anyone have a link to Visual Studio 2005 directcx 2d sprite manipulation or just simple graphics with directx 2d. Jack ...
4
by: Jon Harrop | last post by:
I am writing a 3D graphing component built upon WPF and would like to have 2D vector graphics (e.g. typeset mathematics) as labels laid out from 3D coordinates. For example, a tick on an axis has a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.